Program: 47117
Project: 47117
Buggy source:
/** 
 * Returns a list of strings representing the rescale options for this transcode i.e. the ffmpeg -vf options used to show subtitles in either SSA/ASS or picture-based format and resize a video that's too wide and/or high for the specified renderer. If the renderer has no size limits, or there's no media metadata, or the video is within the renderer's size limits, an empty list is returned.
 * @param dlna
 * @param media metadata for the DLNA resource which is being transcoded
 * @param params
 * @return a {@link List} of <code>String</code>s representing the rescale options for this video,or an empty list if the video doesn't need to be resized.
 * @throws java.io.IOException
 */
public List<String> getVideoFilterOptions(DLNAResource dlna,DLNAMediaInfo media,OutputParams params) throws IOException {
  List<String> videoFilterOptions=new ArrayList<>();
  ArrayList<String> filterChain=new ArrayList<>();
  ArrayList<String> scalePadFilterChain=new ArrayList<>();
  final RendererConfiguration renderer=params.mediaRenderer;
  boolean isMediaValid=media != null && media.isMediaparsed() && media.getHeight() != 0;
  boolean isResolutionTooHighForRenderer=isMediaValid && !params.mediaRenderer.isResolutionCompatibleWithRenderer(media.getWidth(),media.getHeight());
  int scaleWidth=0;
  int scaleHeight=0;
  if (media.getWidth() > 0 && media.getHeight() > 0) {
    scaleWidth=media.getWidth();
    scaleHeight=media.getHeight();
  }
  boolean is3D=media.is3d() && !media.stereoscopyIsAnaglyph();
  boolean keepAR=renderer.isKeepAspectRatio() && !media.is3dFullSbsOrOu() && !"String_Node_Str".equals(media.getAspectRatioContainer());
  if (isResolutionTooHighForRenderer || (!renderer.isRescaleByRenderer() && renderer.isMaximumResolutionSpecified() && media.getWidth() < 720)) {
    if (media.is3dFullSbsOrOu()) {
      scalePadFilterChain.add(String.format("String_Node_Str",renderer.getMaxVideoWidth(),renderer.getMaxVideoHeight()));
    }
 else {
      scalePadFilterChain.add(String.format("String_Node_Str",renderer.getMaxVideoWidth(),renderer.getMaxVideoHeight()));
      if (keepAR) {
        scalePadFilterChain.add(String.format("String_Node_Str",renderer.getMaxVideoWidth(),renderer.getMaxVideoHeight()));
      }
    }
  }
 else   if (keepAR && isMediaValid) {
    if ((media.getWidth() / (double)media.getHeight()) >= (16 / (double)9)) {
      scalePadFilterChain.add("String_Node_Str");
      scaleHeight=(int)Math.round(scaleWidth / (16 / (double)9));
    }
 else {
      scalePadFilterChain.add("String_Node_Str");
      scaleWidth=(int)Math.round(scaleHeight * (16 / (double)9));
    }
    scaleWidth=convertToModX(scaleWidth,4);
    scaleHeight=convertToModX(scaleHeight,4);
    if (scaleHeight > renderer.getMaxVideoHeight() || scaleWidth > renderer.getMaxVideoWidth()) {
      scaleHeight=renderer.getMaxVideoHeight();
      scaleWidth=renderer.getMaxVideoWidth();
    }
    scalePadFilterChain.add("String_Node_Str" + scaleWidth + "String_Node_Str"+ scaleHeight);
  }
  filterChain.addAll(scalePadFilterChain);
  boolean override=true;
  if (renderer instanceof RendererConfiguration.OutputOverride) {
    RendererConfiguration.OutputOverride or=(RendererConfiguration.OutputOverride)renderer;
    override=or.addSubtitles();
  }
  if (!isDisableSubtitles(params) && override) {
    boolean isSubsManualTiming=true;
    StringBuilder subsFilter=new StringBuilder();
    if (params.sid != null && params.sid.getType().isText()) {
      boolean isSubsASS=params.sid.getType() == SubtitleType.ASS;
      String originalSubsFilename=null;
      if (is3D && !isSubsASS) {
        originalSubsFilename=SubtitleUtils.getSubtitles(dlna,media,params,configuration,SubtitleType.ASS).getAbsolutePath();
      }
 else       if (params.sid.isExternal()) {
        originalSubsFilename=params.sid.getExternalFile().getAbsolutePath();
      }
 else       if (params.sid.isEmbedded()) {
        originalSubsFilename=dlna.getSystemName();
      }
      if (originalSubsFilename != null) {
        subsFilter.append("String_Node_Str").append(StringUtil.ffmpegEscape(originalSubsFilename));
        if (params.sid.isEmbedded()) {
          subsFilter.append("String_Node_Str").append(params.sid.getId());
        }
        if (!params.sid.isSubsUtf8()) {
          if (isNotBlank(configuration.getSubtitlesCodepage())) {
            subsFilter.append("String_Node_Str").append(configuration.getSubtitlesCodepage());
          }
 else           if (params.sid.getSubCharacterSet() != null) {
            subsFilter.append("String_Node_Str").append(params.sid.getSubCharacterSet());
          }
        }
        if (configuration.isFFmpegFontConfig() && !is3D && !isSubsASS) {
          subsFilter.append("String_Node_Str");
          subsFilter.append("String_Node_Str");
          subsFilter.append("String_Node_Str").append(configuration.getFont());
          subsFilter.append("String_Node_Str").append((int)15 * Double.parseDouble(configuration.getAssScale()));
          subsFilter.append("String_Node_Str").append(SubtitleUtils.convertColourToASSColourString(configuration.getSubsColor()));
          subsFilter.append("String_Node_Str").append(configuration.getAssOutline());
          subsFilter.append("String_Node_Str").append(configuration.getAssShadow());
          subsFilter.append("String_Node_Str").append(configuration.getAssMargin());
          subsFilter.append("String_Node_Str");
        }
      }
    }
 else     if (params.sid.getType().isPicture()) {
      if (params.sid.getId() < 100) {
        subsFilter.append("String_Node_Str").append(media.getSubtitleTracksList().indexOf(params.sid)).append("String_Node_Str");
        isSubsManualTiming=false;
      }
 else {
        videoFilterOptions.add("String_Node_Str");
        videoFilterOptions.add(params.sid.getExternalFile().getAbsolutePath());
        subsFilter.append("String_Node_Str");
      }
    }
    if (isNotBlank(subsFilter)) {
      if (params.timeseek > 0 && isSubsManualTiming) {
        filterChain.add("String_Node_Str" + params.timeseek + "String_Node_Str");
      }
      filterChain.add(subsFilter.toString());
      if (params.timeseek > 0 && isSubsManualTiming) {
        filterChain.add("String_Node_Str");
      }
    }
  }
  String overrideVF=renderer.getFFmpegVideoFilterOverride();
  if (StringUtils.isNotEmpty(overrideVF)) {
    filterChain.add(overrideVF);
  }
  if (is3D && media.get3DLayout() != null && isNotBlank(params.mediaRenderer.getOutput3DFormat()) && !media.get3DLayout().toString().toLowerCase().equals(params.mediaRenderer.getOutput3DFormat().trim())) {
    filterChain.add("String_Node_Str" + media.get3DLayout().toString().toLowerCase() + "String_Node_Str"+ params.mediaRenderer.getOutput3DFormat().trim().toLowerCase());
  }
  if (filterChain.size() > 0) {
    videoFilterOptions.add("String_Node_Str");
    videoFilterOptions.add(StringUtils.join(filterChain,"String_Node_Str"));
  }
  return videoFilterOptions;
}

Fixed version (ground truth):
/** 
 * Returns a list of strings representing the rescale options for this transcode i.e. the ffmpeg -vf options used to show subtitles in either SSA/ASS or picture-based format and resize a video that's too wide and/or high for the specified renderer. If the renderer has no size limits, or there's no media metadata, or the video is within the renderer's size limits, an empty list is returned.
 * @param dlna
 * @param media metadata for the DLNA resource which is being transcoded
 * @param params
 * @return a {@link List} of <code>String</code>s representing the rescale options for this video,or an empty list if the video doesn't need to be resized.
 * @throws java.io.IOException
 */
public List<String> getVideoFilterOptions(DLNAResource dlna,DLNAMediaInfo media,OutputParams params) throws IOException {
  List<String> videoFilterOptions=new ArrayList<>();
  ArrayList<String> filterChain=new ArrayList<>();
  ArrayList<String> scalePadFilterChain=new ArrayList<>();
  final RendererConfiguration renderer=params.mediaRenderer;
  boolean isMediaValid=media != null && media.isMediaparsed() && media.getHeight() != 0;
  boolean isResolutionTooHighForRenderer=isMediaValid && !params.mediaRenderer.isResolutionCompatibleWithRenderer(media.getWidth(),media.getHeight());
  int scaleWidth=0;
  int scaleHeight=0;
  if (media.getWidth() > 0 && media.getHeight() > 0) {
    scaleWidth=media.getWidth();
    scaleHeight=media.getHeight();
  }
  boolean is3D=media.is3d() && !media.stereoscopyIsAnaglyph();
  boolean keepAR=renderer.isKeepAspectRatio() && !media.is3dFullSbsOrOu() && !"String_Node_Str".equals(media.getAspectRatioContainer());
  if (isResolutionTooHighForRenderer || (!renderer.isRescaleByRenderer() && renderer.isMaximumResolutionSpecified() && media.getWidth() < 720)) {
    if (media.is3dFullSbsOrOu()) {
      scalePadFilterChain.add(String.format("String_Node_Str",renderer.getMaxVideoWidth(),renderer.getMaxVideoHeight()));
    }
 else {
      scalePadFilterChain.add(String.format("String_Node_Str",renderer.getMaxVideoWidth(),renderer.getMaxVideoHeight()));
      if (keepAR) {
        scalePadFilterChain.add(String.format("String_Node_Str",renderer.getMaxVideoWidth(),renderer.getMaxVideoHeight()));
      }
    }
  }
 else   if (keepAR && isMediaValid) {
    if ((media.getWidth() / (double)media.getHeight()) >= (16 / (double)9)) {
      scalePadFilterChain.add("String_Node_Str");
      scaleHeight=(int)Math.round(scaleWidth / (16 / (double)9));
    }
 else {
      scalePadFilterChain.add("String_Node_Str");
      scaleWidth=(int)Math.round(scaleHeight * (16 / (double)9));
    }
    scaleWidth=convertToModX(scaleWidth,4);
    scaleHeight=convertToModX(scaleHeight,4);
    if (scaleHeight > renderer.getMaxVideoHeight() || scaleWidth > renderer.getMaxVideoWidth()) {
      scaleHeight=renderer.getMaxVideoHeight();
      scaleWidth=renderer.getMaxVideoWidth();
    }
    scalePadFilterChain.add("String_Node_Str" + scaleWidth + "String_Node_Str"+ scaleHeight);
  }
  filterChain.addAll(scalePadFilterChain);
  boolean override=true;
  if (renderer instanceof RendererConfiguration.OutputOverride) {
    RendererConfiguration.OutputOverride or=(RendererConfiguration.OutputOverride)renderer;
    override=or.addSubtitles();
  }
  if (!isDisableSubtitles(params) && override) {
    boolean isSubsManualTiming=true;
    StringBuilder subsFilter=new StringBuilder();
    if (params.sid != null && params.sid.getType().isText()) {
      boolean isSubsASS=params.sid.getType() == SubtitleType.ASS;
      String originalSubsFilename=null;
      if (is3D && !isSubsASS) {
        originalSubsFilename=SubtitleUtils.getSubtitles(dlna,media,params,configuration,SubtitleType.ASS).getAbsolutePath();
      }
 else       if (params.sid.isExternal()) {
        originalSubsFilename=params.sid.getExternalFile().getAbsolutePath();
      }
 else       if (params.sid.isEmbedded()) {
        originalSubsFilename=dlna.getSystemName();
      }
      if (originalSubsFilename != null) {
        subsFilter.append("String_Node_Str").append(StringUtil.ffmpegEscape(originalSubsFilename));
        if (params.sid.isEmbedded()) {
          subsFilter.append("String_Node_Str").append(params.sid.getId());
        }
        if (!params.sid.isSubsUtf8()) {
          if (isNotBlank(configuration.getSubtitlesCodepage())) {
            subsFilter.append("String_Node_Str").append(configuration.getSubtitlesCodepage());
          }
 else           if (params.sid.getSubCharacterSet() != null) {
            subsFilter.append("String_Node_Str").append(params.sid.getSubCharacterSet());
          }
        }
        if (configuration.isFFmpegFontConfig() && !is3D && !isSubsASS) {
          subsFilter.append("String_Node_Str");
          subsFilter.append("String_Node_Str");
          String fontName=configuration.getFont();
          if (StringUtils.isNotBlank(fontName)) {
            File fontFile=new File(fontName);
            if (fontFile.exists()) {
              try {
                Font customFont=Font.createFont(Font.TRUETYPE_FONT,fontFile);
                subsFilter.append("String_Node_Str").append(customFont.getName());
              }
 catch (              FontFormatException e) {
                LOGGER.debug("String_Node_Str",e.getMessage());
              }
            }
 else {
              String fonts[]=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
              for (int i=0; i < fonts.length; i++) {
                if (fonts[i].equals(configuration.getFont())) {
                  subsFilter.append("String_Node_Str").append(fontName);
                }
              }
            }
          }
          subsFilter.append("String_Node_Str").append((int)15 * Double.parseDouble(configuration.getAssScale()));
          subsFilter.append("String_Node_Str").append(SubtitleUtils.convertColourToASSColourString(configuration.getSubsColor()));
          subsFilter.append("String_Node_Str").append(configuration.getAssOutline());
          subsFilter.append("String_Node_Str").append(configuration.getAssShadow());
          subsFilter.append("String_Node_Str").append(configuration.getAssMargin());
          subsFilter.append("String_Node_Str");
        }
      }
    }
 else     if (params.sid.getType().isPicture()) {
      if (params.sid.getId() < 100) {
        subsFilter.append("String_Node_Str").append(media.getSubtitleTracksList().indexOf(params.sid)).append("String_Node_Str");
        isSubsManualTiming=false;
      }
 else {
        videoFilterOptions.add("String_Node_Str");
        videoFilterOptions.add(params.sid.getExternalFile().getAbsolutePath());
        subsFilter.append("String_Node_Str");
      }
    }
    if (isNotBlank(subsFilter)) {
      if (params.timeseek > 0 && isSubsManualTiming) {
        filterChain.add("String_Node_Str" + params.timeseek + "String_Node_Str");
      }
      filterChain.add(subsFilter.toString());
      if (params.timeseek > 0 && isSubsManualTiming) {
        filterChain.add("String_Node_Str");
      }
    }
  }
  String overrideVF=renderer.getFFmpegVideoFilterOverride();
  if (StringUtils.isNotEmpty(overrideVF)) {
    filterChain.add(overrideVF);
  }
  if (is3D && media.get3DLayout() != null && isNotBlank(params.mediaRenderer.getOutput3DFormat()) && !media.get3DLayout().toString().toLowerCase().equals(params.mediaRenderer.getOutput3DFormat().trim())) {
    filterChain.add("String_Node_Str" + media.get3DLayout().toString().toLowerCase() + "String_Node_Str"+ params.mediaRenderer.getOutput3DFormat().trim().toLowerCase());
  }
  if (filterChain.size() > 0) {
    videoFilterOptions.add("String_Node_Str");
    videoFilterOptions.add(StringUtils.join(filterChain,"String_Node_Str"));
  }
  return videoFilterOptions;
}