1 /*
2 **
3 ** Copyright (C) 2008 The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #ifndef ANDROID_VIDEO_FRAME_H
19 #define ANDROID_VIDEO_FRAME_H
20 
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <utils/Log.h>
25 
26 namespace android {
27 
28 // Represents a color converted (RGB-based) video frame with bitmap
29 // pixels stored in FrameBuffer.
30 // In a VideoFrame struct stored in IMemory, frame data and ICC data
31 // come after the VideoFrame structure. Their locations can be retrieved
32 // by getFlattenedData() and getFlattenedIccData();
33 class VideoFrame
34 {
35 public:
36     // Construct a VideoFrame object with the specified parameters,
37     // will calculate frame buffer size if |hasData| is set to true.
VideoFrame(uint32_t width,uint32_t height,uint32_t displayWidth,uint32_t displayHeight,uint32_t displayLeft,uint32_t displayTop,uint32_t tileWidth,uint32_t tileHeight,uint32_t angle,uint32_t bpp,uint32_t bitDepth,bool hasData,size_t iccSize)38     VideoFrame(uint32_t width, uint32_t height,
39             uint32_t displayWidth, uint32_t displayHeight,
40             uint32_t displayLeft, uint32_t displayTop,
41             uint32_t tileWidth, uint32_t tileHeight,
42             uint32_t angle, uint32_t bpp, uint32_t bitDepth, bool hasData, size_t iccSize):
43         mWidth(width), mHeight(height),
44         mDisplayWidth(displayWidth), mDisplayHeight(displayHeight),
45         mDisplayLeft(displayLeft), mDisplayTop(displayTop),
46         mTileWidth(tileWidth), mTileHeight(tileHeight), mDurationUs(0),
47         mRotationAngle(angle), mBytesPerPixel(bpp), mIccSize(iccSize),
48         mBitDepth(bitDepth) {
49             uint32_t multVal;
50             mRowBytes = __builtin_mul_overflow(bpp, width, &multVal) ? 0 : multVal;
51             mSize = __builtin_mul_overflow(multVal, height, &multVal) ? 0 : multVal;
52             if (hasData && (mRowBytes == 0 || mSize == 0)) {
53                 ALOGE("Frame rowBytes/ size overflow %dx%d bpp %d", width, height, bpp);
54                 android_errorWriteLog(0x534e4554, "233006499");
55             }
56     }
57 
init(const VideoFrame & copy,const void * iccData,size_t iccSize)58     void init(const VideoFrame& copy, const void* iccData, size_t iccSize) {
59         *this = copy;
60         if (mIccSize == iccSize && iccSize > 0 && iccData != NULL) {
61             memcpy(getFlattenedIccData(), iccData, iccSize);
62         } else {
63             mIccSize = 0;
64         }
65     }
66 
67     // Calculate the flattened size to put it in IMemory
getFlattenedSize()68     size_t getFlattenedSize() const {
69         return sizeof(VideoFrame) + mSize + mIccSize;
70     }
71 
72     // Get the pointer to the frame data in a flattened VideoFrame in IMemory
getFlattenedData()73     uint8_t* getFlattenedData() const {
74         return (uint8_t*)this + sizeof(VideoFrame);
75     }
76 
77     // Get the pointer to the ICC data in a flattened VideoFrame in IMemory
getFlattenedIccData()78     uint8_t* getFlattenedIccData() const {
79         return (uint8_t*)this + sizeof(VideoFrame) + mSize;
80     }
81 
82     // Intentional public access modifier:
83     uint32_t mWidth;           // Decoded image width before rotation
84     uint32_t mHeight;          // Decoded image height before rotation
85     uint32_t mDisplayWidth;    // Display width before rotation
86     uint32_t mDisplayHeight;   // Display height before rotation
87     uint32_t mDisplayLeft;     // Display left (column coordinate) before rotation
88     uint32_t mDisplayTop;      // Display top (row coordinate) before rotation
89     uint32_t mTileWidth;       // Tile width (0 if image doesn't have grid)
90     uint32_t mTileHeight;      // Tile height (0 if image doesn't have grid)
91     int64_t  mDurationUs;      // Frame duration in microseconds
92     int32_t  mRotationAngle;   // Rotation angle, clockwise, should be multiple of 90
93     uint32_t mBytesPerPixel;   // Number of bytes per pixel
94     uint32_t mRowBytes;        // Number of bytes per row before rotation
95     uint32_t mSize;            // Number of bytes of frame data
96     uint32_t mIccSize;         // Number of bytes of ICC data
97     uint32_t mBitDepth;        // number of bits per R / G / B channel
98 };
99 
100 }; // namespace android
101 
102 #endif // ANDROID_VIDEO_FRAME_H
103