Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1f5de383 authored by Chih-Chung Chang's avatar Chih-Chung Chang
Browse files

Display thumbnails one by one and cache them.

Change-Id: I10808f5bca475aea924817bf532dc5916438563a
parent 0a2f9664
Loading
Loading
Loading
Loading
+42 −51
Original line number Diff line number Diff line
@@ -3781,72 +3781,62 @@ class MediaArtistNativeHelper {
     * @param startMs The starting time in ms
     * @param endMs The end time in ms
     * @param thumbnailCount The number of frames to be extracted
     * @param indices The indices of thumbnails wanted
     * @param callback The callback used to pass back the bitmaps
     * from startMs to endMs
     *
     * @return The frames as bitmaps in bitmap array
     **/
    Bitmap[] getPixelsList(String filename, int width, int height, long startMs, long endMs,
            int thumbnailCount) {
        int[] rgb888 = null;
        int thumbnailSize = 0;
        Bitmap tempBitmap = null;

    void getPixelsList(String filename, final int width, final int height,
            long startMs, long endMs, int thumbnailCount, int[] indices,
            final MediaItem.GetThumbnailListCallback callback) {
        /* Make width and height as even */
        final int newWidth = (width + 1) & 0xFFFFFFFE;
        final int newHeight = (height + 1) & 0xFFFFFFFE;
        thumbnailSize = newWidth * newHeight * 4;
        final int thumbnailSize = newWidth * newHeight;

        /* Create a temp bitmap for resized thumbnails */
        if ((newWidth != width) || (newHeight != height)) {
            tempBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
        }
        int i = 0;
        int deltaTime = (int)(endMs - startMs) / thumbnailCount;
        Bitmap[] bitmaps = null;

        try {
            // This may result in out of Memory Error
            rgb888 = new int[thumbnailSize * thumbnailCount];
            bitmaps = new Bitmap[thumbnailCount];
        } catch (Throwable e) {
            // Allocating to new size with Fixed count
            try {
                rgb888 = new int[thumbnailSize * MAX_THUMBNAIL_PERMITTED];
                bitmaps = new Bitmap[MAX_THUMBNAIL_PERMITTED];
                thumbnailCount = MAX_THUMBNAIL_PERMITTED;
            } catch (Throwable ex) {
                throw new RuntimeException("Memory allocation fails, thumbnail count too large: "
                        + thumbnailCount);
            }
        }
        IntBuffer tmpBuffer = IntBuffer.allocate(thumbnailSize);
        nativeGetPixelsList(filename, rgb888, newWidth, newHeight, deltaTime, thumbnailCount,
                startMs, endMs);

        for (; i < thumbnailCount; i++) {
            bitmaps[i] = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            tmpBuffer.put(rgb888, (i * thumbnailSize), thumbnailSize);
        final Bitmap tempBitmap =
                (newWidth != width || newHeight != height)
                ? Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888)
                : null;

        final int[] rgb888 = new int[thumbnailSize];
        final IntBuffer tmpBuffer = IntBuffer.allocate(thumbnailSize);
        nativeGetPixelsList(filename, rgb888, newWidth, newHeight,
                thumbnailCount, startMs, endMs, indices,
                new NativeGetPixelsListCallback() {
            public void onThumbnail(int index) {
                Bitmap bitmap = Bitmap.createBitmap(
                        width, height, Bitmap.Config.ARGB_8888);
                tmpBuffer.put(rgb888, 0, thumbnailSize);
                tmpBuffer.rewind();

                if ((newWidth == width) && (newHeight == height)) {
                bitmaps[i].copyPixelsFromBuffer(tmpBuffer);
                    bitmap.copyPixelsFromBuffer(tmpBuffer);
                } else {
                    /* Copy the out rgb buffer to temp bitmap */
                    tempBitmap.copyPixelsFromBuffer(tmpBuffer);

                    /* Create a canvas to resize */
                final Canvas canvas = new Canvas(bitmaps[i]);
                canvas.drawBitmap(tempBitmap, new Rect(0, 0, newWidth, newHeight),
                    final Canvas canvas = new Canvas(bitmap);
                    canvas.drawBitmap(tempBitmap,
                            new Rect(0, 0, newWidth, newHeight),
                            new Rect(0, 0, width, height), sResizePaint);

                    canvas.setBitmap(null);
                }
                callback.onThumbnail(bitmap, index);
            }
        });

        if (tempBitmap != null) {
            tempBitmap.recycle();
        }
    }

        return bitmaps;
    interface NativeGetPixelsListCallback {
        public void onThumbnail(int index);
    }

    /**
@@ -3957,8 +3947,9 @@ class MediaArtistNativeHelper {
    private native int nativeGetPixels(String fileName, int[] pixelArray, int width, int height,
            long timeMS);

    private native int nativeGetPixelsList(String fileName, int[] pixelArray, int width, int height,
            int timeMS, int nosofTN, long startTimeMs, long endTimeMs);
    private native int nativeGetPixelsList(String fileName, int[] pixelArray,
            int width, int height, int nosofTN, long startTimeMs, long endTimeMs,
            int[] indices, NativeGetPixelsListCallback callback);

    /**
     * Releases the JNI and cleans up the core native module.. Should be called
+10 −16
Original line number Diff line number Diff line
@@ -616,17 +616,18 @@ public class MediaImageItem extends MediaItem {
     * {@inheritDoc}
     */
    @Override
    public Bitmap[] getThumbnailList(int width, int height, long startMs, long endMs,
        int thumbnailCount) throws IOException {
    public void getThumbnailList(int width, int height,
                                 long startMs, long endMs,
                                 int thumbnailCount,
                                 int[] indices,
                                 GetThumbnailListCallback callback)
                                 throws IOException {
        //KenBurns was not applied on this.
        if (getGeneratedImageClip() == null) {
            final Bitmap thumbnail = scaleImage(mFilename, width, height);
            final Bitmap[] thumbnailArray = new Bitmap[thumbnailCount];
            for (int i = 0; i < thumbnailCount; i++) {
                thumbnailArray[i] = thumbnail;
            for (int i = 0; i < indices.length; i++) {
                callback.onThumbnail(thumbnail, i);
            }

            return thumbnailArray;
        } else {
            if (startMs > endMs) {
                throw new IllegalArgumentException("Start time is greater than end time");
@@ -636,15 +637,8 @@ public class MediaImageItem extends MediaItem {
                throw new IllegalArgumentException("End time is greater than file duration");
            }

            if (startMs == endMs) {
                Bitmap[] bitmap = new Bitmap[1];
                bitmap[0] = mMANativeHelper.getPixels(getGeneratedImageClip(),
                    width, height,startMs);
                return bitmap;
            }

            return mMANativeHelper.getPixelsList(getGeneratedImageClip(), width,
                height,startMs,endMs,thumbnailCount);
            mMANativeHelper.getPixelsList(getGeneratedImageClip(), width,
                height, startMs, endMs, thumbnailCount, indices, callback);
        }
    }

+32 −6
Original line number Diff line number Diff line
@@ -564,16 +564,42 @@ public abstract class MediaItem {
     * @param startMs The start of time range in milliseconds
     * @param endMs The end of the time range in milliseconds
     * @param thumbnailCount The thumbnail count
     *
     * @return The array of Bitmaps
     * @param indices The indices of the thumbnails wanted
     * @param callback The callback used to pass back the bitmaps
     *
     * @throws IOException if a file error occurs
     */
    public abstract Bitmap[] getThumbnailList(int width, int height,
    public abstract void getThumbnailList(int width, int height,
                                          long startMs, long endMs,
                                              int thumbnailCount)
                                          int thumbnailCount,
                                          int[] indices,
                                          GetThumbnailListCallback callback)
                                          throws IOException;

    public interface GetThumbnailListCallback {
        public void onThumbnail(Bitmap bitmap, int index);
    }

    // This is for compatibility, only used in tests.
    public Bitmap[] getThumbnailList(int width, int height,
                                     long startMs, long endMs,
                                     int thumbnailCount)
                                     throws IOException {
        final Bitmap[] bitmaps = new Bitmap[thumbnailCount];
        int[] indices = new int[thumbnailCount];
        for (int i = 0; i < thumbnailCount; i++) {
            indices[i] = i;
        }
        getThumbnailList(width, height, startMs, endMs,
                thumbnailCount, indices, new GetThumbnailListCallback() {
            public void onThumbnail(Bitmap bitmap, int index) {
                bitmaps[index] = bitmap;
            }
        });

        return bitmaps;
    }

    /*
     * {@inheritDoc}
     */
+8 −10
Original line number Diff line number Diff line
@@ -293,8 +293,12 @@ public class MediaVideoItem extends MediaItem {
     * {@inheritDoc}
     */
    @Override
    public Bitmap[] getThumbnailList(int width, int height, long startMs,
            long endMs, int thumbnailCount) throws IOException {
    public void getThumbnailList(int width, int height,
                                 long startMs, long endMs,
                                 int thumbnailCount,
                                 int[] indices,
                                 GetThumbnailListCallback callback)
                                 throws IOException {
        if (startMs > endMs) {
            throw new IllegalArgumentException("Start time is greater than end time");
        }
@@ -307,14 +311,8 @@ public class MediaVideoItem extends MediaItem {
            throw new IllegalArgumentException("Invalid dimension");
        }

        if (startMs == endMs) {
            final Bitmap[] bitmap = new Bitmap[1];
            bitmap[0] = mMANativeHelper.getPixels(super.getFilename(), width, height,startMs);
            return bitmap;
        }

        return mMANativeHelper.getPixelsList(super.getFilename(), width,
                height,startMs,endMs,thumbnailCount);
        mMANativeHelper.getPixelsList(super.getFilename(), width,
                height, startMs, endMs, thumbnailCount, indices, callback);
    }

    /*
+0 −3
Original line number Diff line number Diff line
@@ -26,9 +26,6 @@

#define VIDEO_BROWSER_BGR565


#define VIDEO_BROWSER_PREDECODE_TIME 2000    /* In miliseconds */

/*---------------------------- MACROS ----------------------------*/
#define CHECK_PTR(fct, p, err, errValue) \
{ \
Loading