libwebp编码yuv420为webp图片格式

yuv420编码webp

    • libwebp介绍
    • 代码

libwebp介绍

下载地址与文档

代码

	WebPPicture pic_;
	WebPConfig config_;
	WebPMemoryWriter wrt_;

	if (!WebPConfigPreset(&config_, WEBP_PRESET_DEFAULT, 100) ||
		!WebPPictureInit(&pic_)) {
		return -1;  
	}

	config_.lossless = 0;//是否无损
	//config_.quality = 0; //无损时可调节编码复杂度
	config_.method = 0;
	//config_.sns_strength = 0;
	//config_.filter_strength = 0;
	int ret = WebPValidateConfig(&config_);  // not mandatory, but useful

	//pic_.use_argb = 0;
	pic_.width = width;
	pic_.height = height;
	pic_.writer = WebPMemoryWrite;
	pic_.custom_ptr = &wrt_;
	WebPMemoryWriterInit(&wrt_);

	if (!WebPPictureAlloc(&pic_)) return -1;

	// 获取y、u、v三个分量各自数据的指针地址  
	unsigned char *ybase, *ubase, *vbase;
	ybase = frame_buffer->MutableDataY();
	ubase = frame_buffer->MutableDataU();
	vbase = frame_buffer->MutableDataV();
	int ystride = frame_buffer->StrideY();
	int ustride = frame_buffer->StrideU();
	int vstride = frame_buffer->StrideV();

	unsigned char * dest_y = pic_.y;
	unsigned char * dest_u = pic_.u;
	unsigned char * dest_v = pic_.v;
	int dest_ystride = pic_.y_stride;
	int dest_ustride = pic_.uv_stride;
	int dest_vstride = pic_.uv_stride;
	int widthuv = width_ / 2;

	int i;
	for (i = 0; i < height_; i++) {
		memcpy(dest_y,ybase,width_);
		ybase += ystride;
		dest_y += dest_ystride;
	}

	for (i = 0; i < height_/2; i++) {
		memcpy(dest_u, ubase, widthuv);
		memcpy(dest_v, vbase, widthuv);
		ubase += ustride;
		dest_u += dest_ustride;
		vbase += vstride;
		dest_v += dest_vstride;
	}

	//webrtc::ConvertFromI420(frame, webrtc::kABGR, 0, rgba_);

	/*if (destframe_) {
		WebPFree(destframe_);
		destframe_ = 0;
	}*/

	//ret = WebPEncodeRGBA(rgba_, width_, height_, width_ * 4, 90, &destframe_);
	//ret = WebPEncodeLosslessRGBA(rgba_, width_, height_, width_ * 4, &destframe_);

	


	ret = WebPEncode(&config_, &pic_);
	if (!ret) {
		return -1;
	}
	

	*dest = (char*)wrt_.mem; //webp图片输出
	*destsize = wrt_.size; //编码后大小

	WebPMemoryWriterClear(&wrt_);
	WebPPictureFree(&pic_);  

你可能感兴趣的:(音视频)