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 内存顺序是BGRA
    //BGRAToI420 内存顺序是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);
                //写到这儿减少一次内存拷贝
                
                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);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容