Patches for MP4 audio and subtitle bugfixes

Discuss issues related to PS3 Media Server development (only for programmers)

Patches for MP4 audio and subtitle bugfixes

Postby gverdi » Mon Jun 27, 2011 4:31 am

I've been using PMS for about a year now, but when I first started I ran into some very annoying bugs/behaviors which I had to track down and fix. Now that development has surged in recent months, I ran out of excuses for not sending these patches in. I'd really appreciate some of the developers reviewing the notes and patches below, and hopefully including these in the main line.

Environment

I run PMS against a PS3, with a media collection that includes MP4 and MKV movies, many with multiple language audio tracks, multiple embedded subtitles, as well as external subtitles. I prefer to watch movies in their original language with English subtitles if possible, and have tweaked my PMS.conf (included here) to reflect this. With this setup, 85% of the time I can select a movie and have PMS Do The Right Thing, without having to manually browse and select from the #TRANSCODE# folder.

The other 15% of the time, usually the media metadata such as language is missing or incorrect, or the ripper/encoder has not followed best practice by placing the original language audio track first.

Code: Select all
thumbnails = false
alternativeffmpegpath = C:\\Program Files\\PS3 Media Server\\win32\\dvrms\\ffmpeg_MPGMUX.exe
mencoder_ass = true
folders = H:\\Video,H:\\Music
disable_fakesize = true
usecache = false
maximumbitrate = 0
minimized = true
mencoder_mt = true
engines = mencoder,tsmuxer,avsmencoder,mplayeraudio,ffmpegaudio,tsmuxeraudio,mencoderwebvideo,vlcvideo,mplayervideodump,mplayerwebaudio,vlcaudio,ffmpegdvrmsremux,rawthumbs
tsmuxer_preremix_ac3 = true
notranscode = divx
mencoder_remux_ac3 = false
mencoder_encode = keyint=5:vqscale=1:vqmin=2
use_mplayer_for_video_thumbs = false
maxvideobuffer = 300
mencoder_ass_defaultstyle = false
mencoder_fontconfig = true
mencoder_audiosublangs = eng,off;en,off;*,eng;*,en;*,fre;*,ita;*,und;*,off
mencoder_sublangs =
mencoder_audiolangs =
network_interface = eth0
port = 5001
mencoder_intelligent_sync = true
tsmuxer_mux_all_audiotracks = false
mencoder_decode =
tsmuxer_forcefps = true
mencoder_subfribidi = false
mencoder_nooutofsync = true
mencoder_mux_compatible = true
audio_resample = true
mencoder_noass_scale = 2.0
mencoder_noass_subpos = 5
audiobitrate = 384
codec_spec_script =
mencoder_ass_scale = 1.0
mencoder_ass_margin = 5
mencoder_noass_outline = 2
mencoder_noass_blur = 2
mencoder_ass_outline = 2
mencoder_ass_shadow = 2
mencoder_remux_mpeg2 = false


Background and Patches

1. Mencoder MP4 Wrong Audio Bug

When relying on mencoder to transcode subtitles for video, I would hear the wrong audio track being played in an MP4, or even no audio at all. I traced this to an off-by-one error in the audio track index, which passed to mencoder for playback results in the wrong track being selected.

Code: Select all
Index: ps3mediaserver/net/pms/dlna/DLNAMediaInfo.java
===================================================================
--- ps3mediaserver/net/pms/dlna/DLNAMediaInfo.java   (revision 692)
+++ ps3mediaserver/net/pms/dlna/DLNAMediaInfo.java   (working copy)
@@ -497,7 +497,7 @@
                         int a = line.indexOf("(");
                         int b = line.indexOf("):", a);
                         DLNAMediaAudio audio = new DLNAMediaAudio();
-                        if (langId == 0 && (container.equals("avi") || container.equals("ogm") || container.equals("mov") || container.equals("flv"))) {
+                        if (langId == 0 && (container.equals("avi") || container.equals("ogm") || container.equals("mov") || container.equals("flv") || container.equals("mp4"))) {
                            langId++;
                         }
                         audio.id = langId++;
Index: ps3mediaserver/net/pms/dlna/MediaInfoParser.java
===================================================================
--- ps3mediaserver/net/pms/dlna/MediaInfoParser.java   (revision 692)
+++ ps3mediaserver/net/pms/dlna/MediaInfoParser.java   (working copy)
@@ -130,7 +130,7 @@
                         } else {
                            if (step == MediaInfo.StreamKind.Audio) {
                               currentAudioTrack.id = media.audioCodes.size();
-                              if (media.container != null && (media.container.equals(FormatConfiguration.AVI) || media.container.equals(FormatConfiguration.FLV) || media.container.equals(FormatConfiguration.MOV))) {
+                              if (media.container != null && (media.container.equals(FormatConfiguration.AVI) || media.container.equals(FormatConfiguration.FLV) || media.container.equals(FormatConfiguration.MOV) || media.container.equals(FormatConfiguration.MP4))) {
                                  currentAudioTrack.id++;
                               }
                            } else if (step == MediaInfo.StreamKind.Text) {


2. Mencoder Incorrect Switching to tsMuxer on MP4

When playing MKV and MP4 videos with embedded subtitles, I found the MKVs would be transcoded by mencoder and have the correct subtitle chosen according to the Audio/Subtitle priority setting. However, my MP4s with embedded subtitles would go through tsMuxer and end up not displaying subtitles unless manually chosen from the #TRANSCODE# folder. It turned out that only external and not embedded subtitles are being checked when deciding to transcode via mencoder.

The following patch checks for both external and embedded subtitles, and also reworks the related logic to be a little clearer. Note that this is not a big overhead: if no subtitles end up applying, the video will just be remuxed and not transcoded.

Code: Select all
Index: ps3mediaserver/net/pms/dlna/DLNAResource.java
===================================================================
--- ps3mediaserver/net/pms/dlna/DLNAResource.java   (revision 692)
+++ ps3mediaserver/net/pms/dlna/DLNAResource.java   (working copy)
@@ -341,13 +341,18 @@
                if (child.ext != null) {
                   forceTranscode = child.ext.skip(PMS.getConfiguration().getForceTranscode(), defaultRenderer != null ? defaultRenderer.getTranscodedExtensions() : null);
                }
+               
+               boolean hasEmbeddedSubs = false;
+               if (child.media != null)
+                  for(DLNAMediaSubtitle s:child.media.subtitlesCodes)
+                     hasEmbeddedSubs |= s.getSubType().equals("Embedded");
 
                // Force transcoding if
                // 1- MediaInfo support detected the file was not matched with supported codec configs and no SkipTranscode extension forced by user
                // or 2- ForceTranscode extension forced by user
                // or 3- FFmpeg support and the file is not ps3 compatible (need to remove this ?) and no SkipTranscode extension forced by user
-               // or 4- There's some sub files to deal with
-               if ((forceTranscodeV2 && !skipTranscode) || forceTranscode || (!parserV2 && !child.ext.ps3compatible() && !skipTranscode) || (PMS.getConfiguration().getUseSubtitles() && child.srtFile)) {
+               // or 4- There's some sub files or embedded subs to deal with and no SkipTranscode extension forced by user
+               if (forceTranscode || !skipTranscode && (forceTranscodeV2 || (!parserV2 && !child.ext.ps3compatible()) || (PMS.getConfiguration().getUseSubtitles() && child.srtFile) || hasEmbeddedSubs)) {
                   child.player = pl;
                   logger.debug("Switching " + child.getName() + " to player: " + pl.toString());
                }


3. Add Codec-Specific Setting and Fix "-noass"

A small number of MP4 videos would play as if "fast forwarding" and with weird or missing sound. I tracked this down to a problem with ASS subtitles and mencoder, and used the "-noass" parameter as a workaround. When "-noass" kicked in the "fast forwarding" was fixed, but I was surprised by the displayed subtitle fonts being broken. The sizes, settings, etc. were nothing I had chosen. I discovered that the "-noass" implementation just erases all font settings from the mencoder command line before launch, resulting in using the mencoder default font settings, which are usually not ideal and a surprise to the user.

The following patch adds a built-in codec-specific parameter to use the "-noass" setting for MP4 containers with H264 video. It also fixes the implementation of "-noass" to behave as if the ASS Subtitles box on the Transcoding Settings tab were unchecked, and use the non-ASS font settings entered by the user.

Code: Select all
Index: ps3mediaserver/net/pms/encoders/MEncoderVideo.java
===================================================================
--- ps3mediaserver/net/pms/encoders/MEncoderVideo.java   (revision 692)
+++ ps3mediaserver/net/pms/encoders/MEncoderVideo.java   (working copy)
@@ -158,6 +158,7 @@
       + "container == mov :: -mc 0.1 -noass\n" //$NON-NLS-1$
       + "container == rm  :: -mc 0.1\n" //$NON-NLS-1$
       + "container == matroska && framerate == 29.97  :: -nomux -mc 0\n" //$NON-NLS-1$
+      + "container == mp4 && vcodec == h264 :: -mc 0.1 -noass\n" //$NON-NLS-1$
       + "\n" //$NON-NLS-1$
       + Messages.getString("MEncoderVideo.87") //$NON-NLS-1$
       + Messages.getString("MEncoderVideo.88") //$NON-NLS-1$
@@ -1236,7 +1237,16 @@
       }
 
       boolean needAssFixPTS = false;
-
+      
+      boolean foundNoassParam = false;
+      if (media != null) {
+         String sArgs [] = getSpecificCodecOptions(configuration.getCodecSpecificConfig(), media, params, fileName, subString, configuration.isMencoderIntelligentSync(), false);
+         for(String s:sArgs) {
+            if (s.equals("-noass"))
+               foundNoassParam = true;
+         }
+      }
+      
       StringBuilder sb = new StringBuilder();
 
       // Use ASS & Fontconfig flags (and therefore ASS font styles) for all subtitled files except vobsub, embedded, dvd and mp4 container with srt
@@ -1248,6 +1258,7 @@
          !(params.sid.type == DLNAMediaSubtitle.SUBRIP && media.container.equals("mp4")) &&
          !configuration.isMencoderDisableSubs() &&
          configuration.isMencoderAss() &&
+         !foundNoassParam &&
          !dvd &&
          !avisynth()
       ) {
@@ -1293,7 +1304,7 @@
                sb.append("-subfont ").append(font).append(" "); //$NON-NLS-1$ //$NON-NLS-2$
             }
          }
-         if (configuration.isMencoderAss()) {
+         if (configuration.isMencoderAss() && !foundNoassParam) {
             if (!configuration.isMencoderAssDefaultStyle() || (subString != null && params.sid.type != DLNAMediaSubtitle.ASS)) {
                String assSubColor = "ffffff00";
                if (configuration.getSubsColor() != 0) {


Thanks for any feedback!
gverdi
 
Posts: 7
Joined: Sun Jul 11, 2010 1:26 am

Re: Patches for MP4 audio and subtitle bugfixes

Postby Raptor399 » Mon Jun 27, 2011 7:20 am

Thank you for taking the trouble to document and post these patches! :-)
I will not be adding them myself (can't test), but I'm sure one of the others will pick this up.
Raptor399
Project Member
 
Posts: 1916
Joined: Thu Mar 10, 2011 12:06 am

Re: Patches for MP4 audio and subtitle bugfixes

Postby SubJunk » Mon Jun 27, 2011 7:44 am

Thanks for these, I'm looking over them now

Edit: They look good. I'll add them to the next beta
SubJunk
 
Posts: 1210
Joined: Fri Mar 27, 2009 5:25 am

Re: Patches for MP4 audio and subtitle bugfixes

Postby Madoka » Mon Jun 27, 2011 5:01 pm

Thank you for this!! Item #3 was one of my issues too. Looking forward to trying the new versons out!
Madoka
 
Posts: 111
Joined: Fri Feb 19, 2010 3:02 am

Re: Patches for MP4 audio and subtitle bugfixes

Postby SubJunk » Thu Jun 30, 2011 10:18 pm

Patches have been adding to the official SVN so they will be in the next release, thanks again :)
SubJunk
 
Posts: 1210
Joined: Fri Mar 27, 2009 5:25 am


Return to Developers

Who is online

Users browsing this forum: No registered users and 1 guest