Program: 110413
Project: 110413
Buggy source:
/** 
 * Chooses a suitable subset from a number of video formats. <p> A format is filtered (i.e. not selected) if: <ul> <li>  {@code allowedContainerMimeTypes} is non-null and the format does not have one of thepermitted mime types. <li> {@code filterHdFormats} is true and the format is HD.<li>It's determined that the video decoder isn't powerful enough to decode the format. <li>There exists another format of lower resolution whose resolution exceeds the maximum size in pixels that the video can be rendered within the viewport. </ul>
 * @param formatWrappers Wrapped formats from which to select.
 * @param allowedContainerMimeTypes An array of allowed container mime types. Null allows allmime types.
 * @param filterHdFormats True to filter HD formats. False otherwise.
 * @param orientationMayChange True if the video's orientation may change with respect to theviewport during playback.
 * @param viewportWidth The width in pixels of the viewport within which the video will bedisplayed. If the viewport size may change, this should be set to the maximum possible width.
 * @param viewportHeight The height in pixels of the viewport within which the video will bedisplayed. If the viewport size may change, this should be set to the maximum possible height.
 * @return An array holding the indices of the selected formats.
 * @throws DecoderQueryException
 */
public static int[] selectVideoFormats(List<? extends FormatWrapper> formatWrappers,String[] allowedContainerMimeTypes,boolean filterHdFormats,boolean orientationMayChange,int viewportWidth,int viewportHeight) throws DecoderQueryException {
  int maxVideoPixelsToRetain=Integer.MAX_VALUE;
  ArrayList<Integer> selectedIndexList=new ArrayList<>();
  int maxDecodableFrameSize=MediaCodecUtil.maxH264DecodableFrameSize();
  int formatWrapperCount=formatWrappers.size();
  for (int i=0; i < formatWrapperCount; i++) {
    Format format=formatWrappers.get(i).getFormat();
    if (isFormatPlayable(format,allowedContainerMimeTypes,filterHdFormats,maxDecodableFrameSize)) {
      selectedIndexList.add(i);
      if (format.width > 0 && format.height > 0) {
        Point maxVideoSizeInViewport=getMaxVideoSizeInViewport(orientationMayChange,viewportWidth,viewportHeight,format.width,format.height);
        int videoPixels=format.width * format.height;
        if (format.width >= (int)(maxVideoSizeInViewport.x * FRACTION_TO_CONSIDER_FULLSCREEN) && format.height >= (int)(maxVideoSizeInViewport.y * FRACTION_TO_CONSIDER_FULLSCREEN) && videoPixels < maxVideoPixelsToRetain) {
          maxVideoPixelsToRetain=videoPixels;
        }
      }
    }
  }
  for (int i=selectedIndexList.size() - 1; i >= 0; i--) {
    Format format=formatWrappers.get(i).getFormat();
    if (format.width > 0 && format.height > 0 && format.width * format.height > maxVideoPixelsToRetain) {
      selectedIndexList.remove(i);
    }
  }
  return Util.toArray(selectedIndexList);
}

Fixed version (ground truth):
/** 
 * Chooses a suitable subset from a number of video formats. <p> A format is filtered (i.e. not selected) if: <ul> <li>  {@code allowedContainerMimeTypes} is non-null and the format does not have one of thepermitted mime types. <li> {@code filterHdFormats} is true and the format is HD.<li>It's determined that the video decoder isn't powerful enough to decode the format. <li>There exists another format of lower resolution whose resolution exceeds the maximum size in pixels that the video can be rendered within the viewport. </ul>
 * @param formatWrappers Wrapped formats from which to select.
 * @param allowedContainerMimeTypes An array of allowed container mime types. Null allows allmime types.
 * @param filterHdFormats True to filter HD formats. False otherwise.
 * @param orientationMayChange True if the video's orientation may change with respect to theviewport during playback.
 * @param viewportWidth The width in pixels of the viewport within which the video will bedisplayed. If the viewport size may change, this should be set to the maximum possible width.
 * @param viewportHeight The height in pixels of the viewport within which the video will bedisplayed. If the viewport size may change, this should be set to the maximum possible height.
 * @return An array holding the indices of the selected formats.
 * @throws DecoderQueryException
 */
public static int[] selectVideoFormats(List<? extends FormatWrapper> formatWrappers,String[] allowedContainerMimeTypes,boolean filterHdFormats,boolean orientationMayChange,int viewportWidth,int viewportHeight) throws DecoderQueryException {
  int maxVideoPixelsToRetain=Integer.MAX_VALUE;
  ArrayList<Integer> selectedIndexList=new ArrayList<>();
  int maxDecodableFrameSize=MediaCodecUtil.maxH264DecodableFrameSize();
  int formatWrapperCount=formatWrappers.size();
  for (int i=0; i < formatWrapperCount; i++) {
    Format format=formatWrappers.get(i).getFormat();
    if (isFormatPlayable(format,allowedContainerMimeTypes,filterHdFormats,maxDecodableFrameSize)) {
      selectedIndexList.add(i);
      if (format.width > 0 && format.height > 0) {
        Point maxVideoSizeInViewport=getMaxVideoSizeInViewport(orientationMayChange,viewportWidth,viewportHeight,format.width,format.height);
        int videoPixels=format.width * format.height;
        if (format.width >= (int)(maxVideoSizeInViewport.x * FRACTION_TO_CONSIDER_FULLSCREEN) && format.height >= (int)(maxVideoSizeInViewport.y * FRACTION_TO_CONSIDER_FULLSCREEN) && videoPixels < maxVideoPixelsToRetain) {
          maxVideoPixelsToRetain=videoPixels;
        }
      }
    }
  }
  for (int i=selectedIndexList.size() - 1; i >= 0; i--) {
    Format format=formatWrappers.get(selectedIndexList.get(i)).getFormat();
    if (format.width > 0 && format.height > 0 && format.width * format.height > maxVideoPixelsToRetain) {
      selectedIndexList.remove(i);
    }
  }
  return Util.toArray(selectedIndexList);
}