LibYUV轉碼裁剪縮放 C++

  1. 32bgra to yuv420轉換
int transfer_32bgra_to_yuv420(FrameImage* frameImage) {

    int width = frameImage->width;
    int height = frameImage->height;
    int yuvBufSize = width * height * 3 / 2;
    uint8_t* yuvBuf= new uint8_t[yuvBufSize];

    const int y_length = width * height;
    const int32 uv_stride = (width+1) / 2;
    int uv_length = uv_stride * ((height+1) / 2);
    unsigned char *Y_data_Dst_rotate = yuvBuf;
    unsigned char *U_data_Dst_rotate = yuvBuf + y_length;
    unsigned char *V_data_Dst_rotate = U_data_Dst_rotate + uv_length;

    int Dst_Stride_Y = width;
    //注意這兒有個坑,ARGBToI420 內(nèi)存順序是BGRA
    //BGRAToI420 內(nèi)存順序是ARGB
    YOUME_libyuv::ARGBToI420((const uint8*)frameImage->data,
            width*4,
            Y_data_Dst_rotate, Dst_Stride_Y,
            U_data_Dst_rotate, uv_stride,
            V_data_Dst_rotate, uv_stride,
             width, -height);

    memcpy(frameImage->data, yuvBuf, yuvBufSize);
    if(yuvBuf) {
        delete[] yuvBuf;
    }
    return yuvBufSize;
}
  1. yuv420 旋轉和鏡像
void rotation_and_mirror(FrameImage* frameImage, int degree, bool need_mirror) {
    switch(degree) {
        case 90:
        case 180:
        case 270:
        {
            int src_width  = frameImage->width;
            int src_height = frameImage->height;
            
            //copy origin data
            unsigned char *  origdata = NULL;
            unsigned char * Dst_data = (unsigned char *)(frameImage->data);
            int size = (frameImage->width) * (frameImage->height) * 3 / 2;
            origdata = (unsigned char *)tsk_calloc(1, size);
            memcpy(origdata, frameImage->data, size);
            
            //YUV420 image size
            int I420_Y_Size = src_width * src_height;
//            int I420_U_Size = (src_width >> 1) * (src_height >> 1);
            int I420_U_Size = src_width * src_height / 4 ;
            //    int I420_V_Size = I420_U_Size;
            
            unsigned char *Y_data_src = NULL;
            unsigned char *U_data_src = NULL;
            unsigned char *V_data_src = NULL;
            
            int Dst_Stride_Y_rotate;
            int Dst_Stride_U_rotate;
            int Dst_Stride_V_rotate;
            
            int Dst_Stride_Y = src_width;
            int Dst_Stride_U = src_width >> 1;
            int Dst_Stride_V = Dst_Stride_U;
            
            //最終寫入目標
            unsigned char *Y_data_Dst_rotate = Dst_data;
            unsigned char *U_data_Dst_rotate = Dst_data + I420_Y_Size;
            unsigned char *V_data_Dst_rotate = Dst_data + I420_Y_Size + I420_U_Size;
            
            if (degree == YOUME_libyuv::kRotate90 || degree == YOUME_libyuv::kRotate270) {
                Dst_Stride_Y_rotate = src_height;
                Dst_Stride_U_rotate = (src_height+1) >> 1;
                Dst_Stride_V_rotate = Dst_Stride_U_rotate;
            }
            else {
                Dst_Stride_Y_rotate = src_width;
                Dst_Stride_U_rotate = (src_width+1) >> 1;
                Dst_Stride_V_rotate = Dst_Stride_U_rotate;
            }
            
            //mirro
            if(need_mirror){
                Y_data_src = Dst_data;
                U_data_src = Dst_data + I420_Y_Size;
                V_data_src = Dst_data + I420_Y_Size + I420_U_Size;
                
                unsigned char *Y_data_Dst_mirror = origdata;
                unsigned char *U_data_Dst_mirror = origdata + I420_Y_Size;
                unsigned char *V_data_Dst_mirror = origdata + I420_Y_Size + I420_U_Size;
                int Dst_Stride_Y_mirror = src_width;
                int Dst_Stride_U_mirror = (src_width+1) >> 1;
                int Dst_Stride_V_mirror = Dst_Stride_U_mirror;
                YOUME_libyuv::I420Mirror(Y_data_src, Dst_Stride_Y,
                                   U_data_src, Dst_Stride_U,
                                   V_data_src, Dst_Stride_V,
                                   Y_data_Dst_mirror, Dst_Stride_Y_mirror,
                                   U_data_Dst_mirror, Dst_Stride_U_mirror,
                                   V_data_Dst_mirror, Dst_Stride_V_mirror,
                                   src_width, src_height);
                //寫到這兒減少一次內(nèi)存拷貝
                
                YOUME_libyuv::I420Rotate(Y_data_Dst_mirror, Dst_Stride_Y,
                                   U_data_Dst_mirror, Dst_Stride_U,
                                   V_data_Dst_mirror, Dst_Stride_V,
                                   Y_data_Dst_rotate, Dst_Stride_Y_rotate,
                                   U_data_Dst_rotate, Dst_Stride_U_rotate,
                                   V_data_Dst_rotate, Dst_Stride_V_rotate,
                                   src_width, src_height,
                                   (YOUME_libyuv::RotationMode) degree);
                
            }else{
                
                Y_data_src = origdata;
                U_data_src = origdata + I420_Y_Size;
                V_data_src = origdata + I420_Y_Size + I420_U_Size;
                
                YOUME_libyuv::I420Rotate(Y_data_src, Dst_Stride_Y,
                                   U_data_src, Dst_Stride_U,
                                   V_data_src, Dst_Stride_V,
                                   Y_data_Dst_rotate, Dst_Stride_Y_rotate,
                                   U_data_Dst_rotate, Dst_Stride_U_rotate,
                                   V_data_Dst_rotate, Dst_Stride_V_rotate,
                                   src_width, src_height,
                                   (YOUME_libyuv::RotationMode) degree);
            }
            
            if (degree == YOUME_libyuv::kRotate90 || degree == YOUME_libyuv::kRotate270){
                frameImage->width = src_height;
                frameImage->height = src_width;
            }
            tsk_free((void**)&origdata);
        }
        default:
            break;
    }
    
}
  1. yuv420鏡像
void mirror(FrameImage* frameImage) {
    if (NULL == frameImage) {
        TSK_DEBUG_ERROR("Invalid parameter.");
        return;
    }

    int src_width  = frameImage->width;
    int src_height = frameImage->height;

    //copy origin data
    unsigned char *  origdata = NULL;
    unsigned char * Dst_data = (unsigned char *)(frameImage->data);
    int size = (frameImage->width) * (frameImage->height) * 3 / 2;
    origdata = (unsigned char *)tsk_calloc(1, size);
    memcpy(origdata, frameImage->data, size);

    //YUV420 image size
    int I420_Y_Size = src_width * src_height;
    int I420_U_Size = (src_width >> 1) * (src_height >> 1);
//    int I420_V_Size = I420_U_Size;

    unsigned char *Y_data_src = origdata;
    unsigned char *U_data_src = origdata + I420_Y_Size ;
    unsigned char *V_data_src = origdata + I420_Y_Size + I420_U_Size;


    int Src_Stride_Y = src_width;
    int Src_Stride_U = (src_width+1) >> 1;
    int Src_Stride_V = Src_Stride_U;

    //最終寫入目標
    unsigned char *Y_data_Dst_rotate = Dst_data;
    unsigned char *U_data_Dst_rotate = Dst_data + I420_Y_Size;
    unsigned char *V_data_Dst_rotate = Dst_data + I420_Y_Size + I420_U_Size;

    //mirro
    int Dst_Stride_Y_mirror = src_width;
    int Dst_Stride_U_mirror = (src_width+1) >> 1;
    int Dst_Stride_V_mirror = Dst_Stride_U_mirror;
    YOUME_libyuv::I420Mirror(Y_data_src, Src_Stride_Y,
            U_data_src, Src_Stride_U,
            V_data_src, Src_Stride_V,
            Y_data_Dst_rotate, Dst_Stride_Y_mirror,
            U_data_Dst_rotate, Dst_Stride_U_mirror,
            V_data_Dst_rotate, Dst_Stride_V_mirror,
            src_width, src_height);

    tsk_free((void**)&origdata);
}
  1. yuv保持最大可視范圍縮放和裁剪
std::shared_ptr<FrameImage> video_scale_and_crop_yuv(std::shared_ptr<FrameImage> src, int out_width ,int out_height) {
    //////////////////////////////////////////////////////////////////////////
    std::shared_ptr<FrameImage> dest = std::shared_ptr<FrameImage>(new FrameImage(out_width, out_height));
    int src_width_ = src->width;
    int src_height_= src->height;
    int dst_width_ = out_width;
    int dst_height_ = out_height;

    // Making sure that destination frame is of sufficient size.

    // We want to preserve aspect ratio instead of stretching the frame.
    // Therefore, we need to crop the source frame. Calculate the largest center
    // aligned region of the source frame that can be used.
    const int cropped_src_width =
            std::min(src_width_, dst_width_ * src_height_ / dst_height_);
    const int cropped_src_height =
            std::min(src_height_, dst_height_ * src_width_ / dst_width_);
    // Make sure the offsets are even to avoid rounding errors for the U/V planes.
    const int src_offset_x = ((src_width_ - cropped_src_width) / 2) & ~1;
    const int src_offset_y = ((src_height_ - cropped_src_height) / 2) & ~1;

    //YUV420 image size
    int I420_Y_Size = src_width_ * src_height_;
    int I420_U_Size = src_width_ * src_height_ / 4;
    //    int I420_V_Size = I420_U_Size;

    unsigned char *Y_data_src = (unsigned char *) src->data;
    unsigned char *U_data_src = Y_data_src + I420_Y_Size;
    unsigned char *V_data_src = Y_data_src + I420_Y_Size + I420_U_Size;
    int src_stride_Y = src_width_;
    int src_stride_U = (src_width_+1) >> 1;
    int src_stride_V = src_stride_U;

    //最終寫入目標
    int dest_I420_Y_Size = dst_width_ * dst_height_;
    int dest_I420_U_Size = dst_width_ * dst_height_ / 4;
    unsigned char *Y_data_dest = (unsigned char *) dest->data;
    unsigned char *U_data_dest = Y_data_dest + dest_I420_Y_Size;
    unsigned char *V_data_dest = Y_data_dest + dest_I420_Y_Size + dest_I420_U_Size;
    int dest_stride_Y = dst_width_;
    int dest_stride_U = (dst_width_+1) >> 1;
    int dest_stride_V = dest_stride_U;

    const uint8_t* y_ptr =
            Y_data_src +
                    src_offset_y * src_stride_Y +
                    src_offset_x;
    const uint8_t* u_ptr =
            U_data_src +
                    src_offset_y / 2 * src_stride_U +
                    src_offset_x / 2;
    const uint8_t* v_ptr =
            V_data_src +
                    src_offset_y / 2 * src_stride_V +
                    src_offset_x / 2;

    YOUME_libyuv::I420Scale(
            y_ptr,
            src_stride_Y,
            u_ptr,
            src_stride_U,
            v_ptr,
            src_stride_V,
            cropped_src_width, cropped_src_height,
            Y_data_dest,
            dest_stride_Y,
            U_data_dest,
            dest_stride_U,
            V_data_dest,
            dest_stride_V,
            dst_width_, dst_height_,
            YOUME_libyuv::kFilterBilinear);

    return dest;

    
}

存儲YUV 420的類申明

class FrameImage
{
private:
    bool need_delete_data=false;
public:
    int width;
    int height;
    void* data;
    
public:
    FrameImage(int width, int height, void* data);
    FrameImage(int width, int height);
    ~FrameImage();
};

FrameImage::FrameImage(int width, int height, void* data) {
    this->height = height;
    this->width = width;
    this->data = data;
}

FrameImage::FrameImage(int width, int height) {
    this->height = height;
    this->width = width;
    int size = this->height * this->width * 3 / 2;
    this->data = malloc(size);
    memset(this->data, 128, size);
    need_delete_data = true;
}

FrameImage::~FrameImage(){
    if(need_delete_data){
        free(this->data);
    }
}
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市肢簿,隨后出現(xiàn)的幾起案子勇垛,更是在濱河造成了極大的恐慌镰烧,老刑警劉巖茂附,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異验夯,居然都是意外死亡郊楣,警方通過查閱死者的電腦和手機憔恳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來净蚤,“玉大人钥组,你說我怎么就攤上這事〗衿伲” “怎么了程梦?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長橘荠。 經(jīng)常有香客問我屿附,道長,這世上最難降的妖魔是什么哥童? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任挺份,我火速辦了婚禮,結果婚禮上如蚜,老公的妹妹穿的比我還像新娘压恒。我一直安慰自己,他們只是感情好错邦,可當我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著型宙,像睡著了一般撬呢。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上妆兑,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天魂拦,我揣著相機與錄音毛仪,去河邊找鬼。 笑死芯勘,一個胖子當著我的面吹牛箱靴,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播荷愕,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼衡怀,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了安疗?” 一聲冷哼從身側響起抛杨,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎荐类,沒想到半個月后怖现,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡玉罐,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年屈嗤,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吊输。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡饶号,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出璧亚,到底是詐尸還是另有隱情讨韭,我是刑警寧澤,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布癣蟋,位于F島的核電站透硝,受9級特大地震影響,放射性物質發(fā)生泄漏疯搅。R本人自食惡果不足惜濒生,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望幔欧。 院中可真熱鬧罪治,春花似錦、人聲如沸礁蔗。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽浴井。三九已至晒骇,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背洪囤。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工徒坡, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人瘤缩。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓喇完,卻偏偏與公主長得像,于是被迫代替她去往敵國和親剥啤。 傳聞我的和親對象是個殘疾皇子锦溪,可洞房花燭夜當晚...
    茶點故事閱讀 45,435評論 2 359

推薦閱讀更多精彩內(nèi)容