From 2ce1b6d7fbabc27a391ad92927d803a1c1804ac2 Mon Sep 17 00:00:00 2001
From: John Stebbins <jstebbins.hb@gmail.com>
Date: Wed, 30 May 2018 15:00:04 -0700
Subject: [PATCH] scan: rationalize ffmpeg codec/profile names

ffmpeg is inconsistent in the format of returned profile names.
Sometimes the profile name includes the codec name and sometimes it does
not.  So search for the codec name in the profile name and add codec
name if it's not there.  Ugh!

Fixes https://github.com/HandBrake/HandBrake/issues/1375
---
 libhb/scan.c | 111 ++++++++++++++++++++++++++++++---------------------
 1 file changed, 66 insertions(+), 45 deletions(-)

diff --git a/libhb/scan.c b/libhb/scan.c
index 5b3b0734d..8c35ddbe8 100644
--- a/libhb/scan.c
+++ b/libhb/scan.c
@@ -1187,7 +1187,8 @@ static void LookForAudio(hb_scan_t *scan, hb_title_t * title, hb_buffer_t * b)
     audio->config.in.mode = info.mode;
 
     // now that we have all the info, set the audio description
-    const char *codec_name = NULL;
+    const char *codec_name   = NULL;
+    const char *profile_name = NULL;
     if (audio->config.in.codec & HB_ACODEC_FF_MASK)
     {
         AVCodec *codec = avcodec_find_decoder(audio->config.in.codec_param);
@@ -1195,50 +1196,56 @@ static void LookForAudio(hb_scan_t *scan, hb_title_t * title, hb_buffer_t * b)
         {
             if (info.profile != FF_PROFILE_UNKNOWN)
             {
-                codec_name = av_get_profile_name(codec, info.profile);
+                profile_name = av_get_profile_name(codec, info.profile);
             }
-            if (codec_name == NULL)
+
+            // use our own capitalization for the most common codecs
+            switch (audio->config.in.codec_param)
             {
-                // use our own capitalization for the most common codecs
-                switch (audio->config.in.codec_param)
-                {
-                    case AV_CODEC_ID_AAC:
-                        codec_name = "AAC";
-                        break;
-                    case AV_CODEC_ID_AC3:
-                        codec_name = "AC3";
-                        break;
-                    case AV_CODEC_ID_EAC3:
-                        codec_name = "E-AC3";
-                        break;
-                    case AV_CODEC_ID_TRUEHD:
-                        codec_name = "TrueHD";
-                        break;
-                    case AV_CODEC_ID_DTS:
-                        codec_name = audio->config.in.codec == HB_ACODEC_DCA_HD ? "DTS-HD" : "DTS";
-                        break;
-                    case AV_CODEC_ID_FLAC:
-                        codec_name = "FLAC";
-                        break;
-                    case AV_CODEC_ID_MP2:
-                        codec_name = "MPEG";
-                        break;
-                    case AV_CODEC_ID_MP3:
-                        codec_name = "MP3";
-                        break;
-                    case AV_CODEC_ID_PCM_BLURAY:
-                        codec_name = "BD LPCM";
-                        break;
-                    case AV_CODEC_ID_OPUS:
-                        codec_name = "Opus";
-                        break;
-                    case AV_CODEC_ID_VORBIS:
-                        codec_name = "Vorbis";
-                        break;
-                    default:
-                        codec_name = codec->name;
-                        break;
-                }
+                case AV_CODEC_ID_AAC:
+                    codec_name = "AAC";
+                    break;
+                case AV_CODEC_ID_AC3:
+                    codec_name = "AC3";
+                    break;
+                case AV_CODEC_ID_EAC3:
+                    codec_name = "E-AC3";
+                    break;
+                case AV_CODEC_ID_TRUEHD:
+                    codec_name = "TrueHD";
+                    break;
+                case AV_CODEC_ID_DTS:
+                    if (profile_name == NULL &&
+                        audio->config.in.codec == HB_ACODEC_DCA_HD)
+                        codec_name = "DTS-HD";
+                    else
+                        codec_name = "DTS";
+                    break;
+                case AV_CODEC_ID_FLAC:
+                    codec_name = "FLAC";
+                    break;
+                case AV_CODEC_ID_MP2:
+                    codec_name = "MPEG";
+                    break;
+                case AV_CODEC_ID_MP3:
+                    codec_name = "MP3";
+                    break;
+                case AV_CODEC_ID_PCM_BLURAY:
+                    codec_name = "BD LPCM";
+                    break;
+                case AV_CODEC_ID_OPUS:
+                    codec_name = "Opus";
+                    break;
+                case AV_CODEC_ID_VORBIS:
+                    codec_name = "Vorbis";
+                    break;
+                default:
+                    codec_name = codec->name;
+                    break;
+            }
+            if (strstr(profile_name, codec_name) != NULL)
+            {
+                codec_name = NULL;
             }
         }
         else
@@ -1290,8 +1297,22 @@ static void LookForAudio(hb_scan_t *scan, hb_title_t * title, hb_buffer_t * b)
                 break;
         }
     }
-    sprintf(audio->config.lang.description, "%s (%s)",
-            audio->config.lang.simple, codec_name);
+
+    if (codec_name != NULL && profile_name != NULL)
+    {
+        sprintf(audio->config.lang.description, "%s (%s %s)",
+                audio->config.lang.simple, codec_name, profile_name);
+    }
+    else if (codec_name != NULL)
+    {
+        sprintf(audio->config.lang.description, "%s (%s)",
+                audio->config.lang.simple, codec_name);
+    }
+    else if (profile_name != NULL)
+    {
+        sprintf(audio->config.lang.description, "%s (%s)",
+                audio->config.lang.simple, profile_name);
+    }
 
     if (audio->config.lang.attributes & HB_AUDIO_ATTR_VISUALLY_IMPAIRED)
     {
-- 
2.40.0