Program: 47284
Project: 47284
Buggy source:
@Deprecated protected ProcessWrapperImpl getFFMpegTranscode(String fileName,DLNAResource dlna,DLNAMediaInfo media,OutputParams params,String args[]) throws IOException {
  int nThreads=PMS.getConfiguration().getNumberOfCpuCores();
  List<String> cmdList=new ArrayList<String>();
  RendererConfiguration renderer=params.mediaRenderer;
  cmdList.add(executable());
  cmdList.add("String_Node_Str");
  cmdList.add("String_Node_Str");
  if (params.timeseek > 0) {
    cmdList.add("String_Node_Str");
    cmdList.add("String_Node_Str" + params.timeseek);
  }
  cmdList.add("String_Node_Str");
  cmdList.add("String_Node_Str" + nThreads);
  final boolean isTSMuxerVideoEngineEnabled=PMS.getConfiguration().getEnginesAsList(PMS.get().getRegistry()).contains(TSMuxerVideo.ID);
  ac3Remux=false;
  dtsRemux=false;
  if (PMS.getConfiguration().isRemuxAC3() && params.aid != null && params.aid.isAC3() && !avisynth() && params.mediaRenderer.isTranscodeToAC3()) {
    ac3Remux=true;
  }
 else {
    dtsRemux=isTSMuxerVideoEngineEnabled && PMS.getConfiguration().isDTSEmbedInPCM() && params.aid != null && params.aid.isDTS() && !avisynth() && params.mediaRenderer.isDTSPlayable();
  }
  cmdList.add("String_Node_Str");
  cmdList.add(fileName);
  cmdList.add("String_Node_Str");
  cmdList.add("String_Node_Str" + nThreads);
  if (params.timeend > 0) {
    cmdList.add("String_Node_Str");
    cmdList.add("String_Node_Str" + params.timeend);
  }
  cmdList.addAll(getRescaleOptions(renderer,media));
  int defaultMaxBitrates[]=getVideoBitrateConfig(PMS.getConfiguration().getMaximumBitrate());
  int rendererMaxBitrates[]=new int[2];
  if (renderer.getMaxVideoBitrate() != null) {
    rendererMaxBitrates=getVideoBitrateConfig(renderer.getMaxVideoBitrate());
  }
  if ((defaultMaxBitrates[0] == 0 && rendererMaxBitrates[0] > 0) || rendererMaxBitrates[0] < defaultMaxBitrates[0] && rendererMaxBitrates[0] > 0) {
    defaultMaxBitrates=rendererMaxBitrates;
  }
  if (params.mediaRenderer.getCBRVideoBitrate() == 0 && defaultMaxBitrates[0] > 0) {
    defaultMaxBitrates[0]=1000 * defaultMaxBitrates[0];
    defaultMaxBitrates[0]=defaultMaxBitrates[0] / 2;
    int bufSize=1835;
    if (media.isHDVideo()) {
      bufSize=defaultMaxBitrates[0] / 3;
    }
    if (bufSize > 7000) {
      bufSize=7000;
    }
    if (defaultMaxBitrates[1] > 0) {
      bufSize=defaultMaxBitrates[1];
    }
    if (params.mediaRenderer.isDefaultVBVSize() && rendererMaxBitrates[1] == 0) {
      bufSize=1835;
    }
    defaultMaxBitrates[0]=defaultMaxBitrates[0] - PMS.getConfiguration().getAudioBitrate();
    defaultMaxBitrates[0]=defaultMaxBitrates[0] / 1000 * 1000;
    bufSize=bufSize * 1000;
    defaultMaxBitrates[0]=defaultMaxBitrates[0] * 1000;
    cmdList.add("String_Node_Str");
    cmdList.add("String_Node_Str" + bufSize);
    cmdList.add("String_Node_Str");
    cmdList.add("String_Node_Str" + defaultMaxBitrates[0]);
  }
  int channels;
  if (ac3Remux) {
    channels=params.aid.getAudioProperties().getNumberOfChannels();
  }
 else   if (dtsRemux) {
    channels=2;
  }
 else {
    channels=PMS.getConfiguration().getAudioChannelCount();
  }
  LOGGER.trace("String_Node_Str" + channels);
  if (!(params.aid.isAC3() && !ac3Remux) && !(type() == Format.AUDIO)) {
    cmdList.add("String_Node_Str");
    cmdList.add(PMS.getConfiguration().getAudioBitrate() + "String_Node_Str");
  }
  cmdList.addAll(getCustomArgs());
  cmdList.addAll(getTranscodeVideoOptions(renderer,media,params));
  if (!dtsRemux) {
    cmdList.add("String_Node_Str");
  }
  String[] cmdArray=new String[cmdList.size()];
  cmdList.toArray(cmdArray);
  cmdArray=finalizeTranscoderArgs(fileName,dlna,media,params,cmdArray);
  ProcessWrapperImpl pw=new ProcessWrapperImpl(cmdArray,params);
  if (dtsRemux) {
    PipeProcess pipe;
    pipe=new PipeProcess(System.currentTimeMillis() + "String_Node_Str");
    TSMuxerVideo ts=new TSMuxerVideo(PMS.getConfiguration());
    File f=new File(PMS.getConfiguration().getTempFolder(),"String_Node_Str");
    String cmd[]=new String[]{ts.executable(),f.getAbsolutePath(),pipe.getInputPipe()};
    pw=new ProcessWrapperImpl(cmd,params);
    PipeIPCProcess ffVideoPipe=new PipeIPCProcess(System.currentTimeMillis() + "String_Node_Str",System.currentTimeMillis() + "String_Node_Str",false,true);
    cmdList.add(ffVideoPipe.getInputPipe());
    OutputParams ffparams=new OutputParams(PMS.getConfiguration());
    ffparams.maxBufferSize=1;
    ffparams.stdin=params.stdin;
    String[] cmdArrayDts=new String[cmdList.size()];
    cmdList.toArray(cmdArrayDts);
    cmdArrayDts=finalizeTranscoderArgs(this,fileName,dlna,media,params,cmdArrayDts);
    ProcessWrapperImpl ffVideo=new ProcessWrapperImpl(cmdArrayDts,ffparams);
    ProcessWrapper ff_video_pipe_process=ffVideoPipe.getPipeProcess();
    pw.attachProcess(ff_video_pipe_process);
    ff_video_pipe_process.runInNewThread();
    ffVideoPipe.deleteLater();
    pw.attachProcess(ffVideo);
    ffVideo.runInNewThread();
    PipeIPCProcess ffAudioPipe=new PipeIPCProcess(System.currentTimeMillis() + "String_Node_Str",System.currentTimeMillis() + "String_Node_Str",false,true);
    StreamModifier sm=new StreamModifier();
    sm.setPcm(false);
    sm.setDtsembed(dtsRemux);
    sm.setSampleFrequency(48000);
    sm.setBitspersample(16);
    sm.setNbchannels(channels);
    String ffmpegLPCMextract[]=new String[]{executable(),"String_Node_Str","String_Node_Str","String_Node_Str",fileName,"String_Node_Str","String_Node_Str" + channels,"String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str",ffAudioPipe.getInputPipe()};
    if (!params.mediaRenderer.isMuxDTSToMpeg()) {
      ffAudioPipe.setModifier(sm);
    }
    if (params.stdin != null) {
      ffmpegLPCMextract[3]="String_Node_Str";
    }
    if (params.timeseek > 0) {
      ffmpegLPCMextract[2]="String_Node_Str" + params.timeseek;
    }
    OutputParams ffaudioparams=new OutputParams(PMS.getConfiguration());
    ffaudioparams.maxBufferSize=1;
    ffaudioparams.stdin=params.stdin;
    ProcessWrapperImpl ffAudio=new ProcessWrapperImpl(ffmpegLPCMextract,ffaudioparams);
    params.stdin=null;
    PrintWriter pwMux=new PrintWriter(f);
    pwMux.println("String_Node_Str");
    String videoType="String_Node_Str";
    if (params.no_videoencode && params.forceType != null) {
      videoType=params.forceType;
    }
    String fps="String_Node_Str";
    if (params.forceFps != null) {
      fps="String_Node_Str" + params.forceFps + "String_Node_Str";
    }
    String audioType="String_Node_Str";
    if (dtsRemux) {
      if (params.mediaRenderer.isMuxDTSToMpeg()) {
        audioType="String_Node_Str";
      }
 else {
        audioType="String_Node_Str";
      }
    }
    pwMux.println(videoType + "String_Node_Str" + ffVideoPipe.getOutputPipe()+ "String_Node_Str"+ fps+ "String_Node_Str");
    pwMux.println(audioType + "String_Node_Str" + ffAudioPipe.getOutputPipe()+ "String_Node_Str");
    pwMux.close();
    ProcessWrapper pipe_process=pipe.getPipeProcess();
    pw.attachProcess(pipe_process);
    pipe_process.runInNewThread();
    try {
      Thread.sleep(50);
    }
 catch (    InterruptedException e) {
    }
    pipe.deleteLater();
    params.input_pipes[0]=pipe;
    ProcessWrapper ff_pipe_process=ffAudioPipe.getPipeProcess();
    pw.attachProcess(ff_pipe_process);
    ff_pipe_process.runInNewThread();
    try {
      Thread.sleep(50);
    }
 catch (    InterruptedException e) {
    }
    ffAudioPipe.deleteLater();
    pw.attachProcess(ffAudio);
    ffAudio.runInNewThread();
  }
  pw.runInNewThread();
  return pw;
}

Fixed version (ground truth):
@Deprecated protected ProcessWrapperImpl getFFMpegTranscode(String fileName,DLNAResource dlna,DLNAMediaInfo media,OutputParams params,String args[]) throws IOException {
  int nThreads=PMS.getConfiguration().getNumberOfCpuCores();
  List<String> cmdList=new ArrayList<String>();
  RendererConfiguration renderer=params.mediaRenderer;
  cmdList.add(executable());
  cmdList.add("String_Node_Str");
  cmdList.add("String_Node_Str");
  cmdList.add("String_Node_Str");
  if (params.timeseek > 0) {
    cmdList.add("String_Node_Str");
    cmdList.add("String_Node_Str" + params.timeseek);
  }
  cmdList.add("String_Node_Str");
  cmdList.add("String_Node_Str" + nThreads);
  final boolean isTSMuxerVideoEngineEnabled=PMS.getConfiguration().getEnginesAsList(PMS.get().getRegistry()).contains(TSMuxerVideo.ID);
  ac3Remux=false;
  dtsRemux=false;
  if (PMS.getConfiguration().isRemuxAC3() && params.aid != null && params.aid.isAC3() && !avisynth() && params.mediaRenderer.isTranscodeToAC3()) {
    ac3Remux=true;
  }
 else {
    dtsRemux=isTSMuxerVideoEngineEnabled && PMS.getConfiguration().isDTSEmbedInPCM() && params.aid != null && params.aid.isDTS() && !avisynth() && params.mediaRenderer.isDTSPlayable();
  }
  cmdList.add("String_Node_Str");
  cmdList.add(fileName);
  cmdList.add("String_Node_Str");
  cmdList.add("String_Node_Str" + nThreads);
  if (params.timeend > 0) {
    cmdList.add("String_Node_Str");
    cmdList.add("String_Node_Str" + params.timeend);
  }
  cmdList.addAll(getRescaleOptions(renderer,media));
  int defaultMaxBitrates[]=getVideoBitrateConfig(PMS.getConfiguration().getMaximumBitrate());
  int rendererMaxBitrates[]=new int[2];
  if (renderer.getMaxVideoBitrate() != null) {
    rendererMaxBitrates=getVideoBitrateConfig(renderer.getMaxVideoBitrate());
  }
  if ((defaultMaxBitrates[0] == 0 && rendererMaxBitrates[0] > 0) || rendererMaxBitrates[0] < defaultMaxBitrates[0] && rendererMaxBitrates[0] > 0) {
    defaultMaxBitrates=rendererMaxBitrates;
  }
  if (params.mediaRenderer.getCBRVideoBitrate() == 0 && defaultMaxBitrates[0] > 0) {
    defaultMaxBitrates[0]=1000 * defaultMaxBitrates[0];
    defaultMaxBitrates[0]=defaultMaxBitrates[0] / 2;
    int bufSize=1835;
    if (media.isHDVideo()) {
      bufSize=defaultMaxBitrates[0] / 3;
    }
    if (bufSize > 7000) {
      bufSize=7000;
    }
    if (defaultMaxBitrates[1] > 0) {
      bufSize=defaultMaxBitrates[1];
    }
    if (params.mediaRenderer.isDefaultVBVSize() && rendererMaxBitrates[1] == 0) {
      bufSize=1835;
    }
    defaultMaxBitrates[0]=defaultMaxBitrates[0] - PMS.getConfiguration().getAudioBitrate();
    defaultMaxBitrates[0]=defaultMaxBitrates[0] / 1000 * 1000;
    bufSize=bufSize * 1000;
    defaultMaxBitrates[0]=defaultMaxBitrates[0] * 1000;
    cmdList.add("String_Node_Str");
    cmdList.add("String_Node_Str" + bufSize);
    cmdList.add("String_Node_Str");
    cmdList.add("String_Node_Str" + defaultMaxBitrates[0]);
  }
  int channels;
  if (ac3Remux) {
    channels=params.aid.getAudioProperties().getNumberOfChannels();
  }
 else   if (dtsRemux) {
    channels=2;
  }
 else {
    channels=PMS.getConfiguration().getAudioChannelCount();
  }
  LOGGER.trace("String_Node_Str" + channels);
  if (!(params.aid.isAC3() && !ac3Remux) && !(type() == Format.AUDIO)) {
    cmdList.add("String_Node_Str");
    cmdList.add(PMS.getConfiguration().getAudioBitrate() + "String_Node_Str");
  }
  cmdList.addAll(getCustomArgs());
  cmdList.addAll(getTranscodeVideoOptions(renderer,media,params));
  if (!dtsRemux) {
    cmdList.add("String_Node_Str");
  }
  String[] cmdArray=new String[cmdList.size()];
  cmdList.toArray(cmdArray);
  cmdArray=finalizeTranscoderArgs(fileName,dlna,media,params,cmdArray);
  ProcessWrapperImpl pw=new ProcessWrapperImpl(cmdArray,params);
  if (dtsRemux) {
    PipeProcess pipe;
    pipe=new PipeProcess(System.currentTimeMillis() + "String_Node_Str");
    TSMuxerVideo ts=new TSMuxerVideo(PMS.getConfiguration());
    File f=new File(PMS.getConfiguration().getTempFolder(),"String_Node_Str");
    String cmd[]=new String[]{ts.executable(),f.getAbsolutePath(),pipe.getInputPipe()};
    pw=new ProcessWrapperImpl(cmd,params);
    PipeIPCProcess ffVideoPipe=new PipeIPCProcess(System.currentTimeMillis() + "String_Node_Str",System.currentTimeMillis() + "String_Node_Str",false,true);
    cmdList.add(ffVideoPipe.getInputPipe());
    OutputParams ffparams=new OutputParams(PMS.getConfiguration());
    ffparams.maxBufferSize=1;
    ffparams.stdin=params.stdin;
    String[] cmdArrayDts=new String[cmdList.size()];
    cmdList.toArray(cmdArrayDts);
    cmdArrayDts=finalizeTranscoderArgs(this,fileName,dlna,media,params,cmdArrayDts);
    ProcessWrapperImpl ffVideo=new ProcessWrapperImpl(cmdArrayDts,ffparams);
    ProcessWrapper ff_video_pipe_process=ffVideoPipe.getPipeProcess();
    pw.attachProcess(ff_video_pipe_process);
    ff_video_pipe_process.runInNewThread();
    ffVideoPipe.deleteLater();
    pw.attachProcess(ffVideo);
    ffVideo.runInNewThread();
    PipeIPCProcess ffAudioPipe=new PipeIPCProcess(System.currentTimeMillis() + "String_Node_Str",System.currentTimeMillis() + "String_Node_Str",false,true);
    StreamModifier sm=new StreamModifier();
    sm.setPcm(false);
    sm.setDtsembed(dtsRemux);
    sm.setSampleFrequency(48000);
    sm.setBitspersample(16);
    sm.setNbchannels(channels);
    String ffmpegLPCMextract[]=new String[]{executable(),"String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str",fileName,"String_Node_Str","String_Node_Str" + channels,"String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str",ffAudioPipe.getInputPipe()};
    if (!params.mediaRenderer.isMuxDTSToMpeg()) {
      ffAudioPipe.setModifier(sm);
    }
    if (params.stdin != null) {
      ffmpegLPCMextract[4]="String_Node_Str";
    }
    if (params.timeseek > 0) {
      ffmpegLPCMextract[3]="String_Node_Str" + params.timeseek;
    }
    OutputParams ffaudioparams=new OutputParams(PMS.getConfiguration());
    ffaudioparams.maxBufferSize=1;
    ffaudioparams.stdin=params.stdin;
    ProcessWrapperImpl ffAudio=new ProcessWrapperImpl(ffmpegLPCMextract,ffaudioparams);
    params.stdin=null;
    PrintWriter pwMux=new PrintWriter(f);
    pwMux.println("String_Node_Str");
    String videoType="String_Node_Str";
    if (params.no_videoencode && params.forceType != null) {
      videoType=params.forceType;
    }
    String fps="String_Node_Str";
    if (params.forceFps != null) {
      fps="String_Node_Str" + params.forceFps + "String_Node_Str";
    }
    String audioType="String_Node_Str";
    if (dtsRemux) {
      if (params.mediaRenderer.isMuxDTSToMpeg()) {
        audioType="String_Node_Str";
      }
 else {
        audioType="String_Node_Str";
      }
    }
    pwMux.println(videoType + "String_Node_Str" + ffVideoPipe.getOutputPipe()+ "String_Node_Str"+ fps+ "String_Node_Str");
    pwMux.println(audioType + "String_Node_Str" + ffAudioPipe.getOutputPipe()+ "String_Node_Str");
    pwMux.close();
    ProcessWrapper pipe_process=pipe.getPipeProcess();
    pw.attachProcess(pipe_process);
    pipe_process.runInNewThread();
    try {
      Thread.sleep(50);
    }
 catch (    InterruptedException e) {
    }
    pipe.deleteLater();
    params.input_pipes[0]=pipe;
    ProcessWrapper ff_pipe_process=ffAudioPipe.getPipeProcess();
    pw.attachProcess(ff_pipe_process);
    ff_pipe_process.runInNewThread();
    try {
      Thread.sleep(50);
    }
 catch (    InterruptedException e) {
    }
    ffAudioPipe.deleteLater();
    pw.attachProcess(ffAudio);
    ffAudio.runInNewThread();
  }
  pw.runInNewThread();
  return pw;
}