ffmpeg 图像格式转换

ffmpeg 将AVFrame转换为dscFrame格式

av_image_to(pFrame, &dscFrame, AV_PIX_FMT_YUVJ420P, pFrame->width,
				pFrame->height);
int av_image_to(AVFrame *pFrame, AVFrame **pdscFrame, int dscFormat, int dscW,
		int dscH) {
	struct SwsContext *m_pSwsContext;
	AVFrame *frame = av_frame_alloc();
	int ret = 0;

	frame->format = dscFormat; //pFrame->format;
	frame->width = dscW;
	frame->height = dscH;

	//	int x, y;  //nv12->yuv420
	//	for (y = 0; y < frame->height; y++) {
	//		for (x = 0; x < frame->width; x++) {
	//			frame->data[0][y * frame->linesize[0] + x] = pFrame->data[0][(y + 0)
	//			* pFrame->linesize[0] + 0 + x];
	//		}
	//	}
	//	for (y = 0; y < frame->height / 2; y++) {
	//		for (x = 0; x < frame->width / 2; x++) {
	//			frame->data[1][y * frame->linesize[1] + x] = pFrame->data[1][(y + 0)* pFrame->linesize[1] * 2 + 0 + x * 2];
	//			frame->data[2][y * frame->linesize[2] + x] = pFrame->data[1][(y + 0)* pFrame->linesize[1] * 2 + 0 + x * 2 + 1];
	//		}
	//	}
	if (av_frame_get_buffer(frame, 1) < 0) {
		printf("pickupPic av_frame_get_buffer failed ! \n");
		av_frame_free(&frame);
		return -1;
	}

	if (av_frame_make_writable(frame) < 0) {
		printf("pickupPic av_frame_make_writable failed ! \n");
		av_frame_free(&frame);
		return -1;
	}

	m_pSwsContext = sws_getContext(pFrame->width, pFrame->height,
			(enum AVPixelFormat) pFrame->format, dscW, dscH, dscFormat,
			SWS_BICUBIC, NULL, NULL,
			NULL);

	ret = sws_scale(m_pSwsContext, pFrame->data, pFrame->linesize, 0,
			pFrame->height, frame->data, frame->linesize);

	sws_freeContext(m_pSwsContext);

	*pdscFrame = frame;
	return ret;
}
//ffmpeg -pix_fmt nv12 -s 1920x1080 -i 2019-06-03_19_14_25_018_1920x1080.yuv aa.jpg
//ffmpeg -pix_fmt nv12 -s 1920x1080 -i aa.jpg yuv.yuv
//ffmpeg -pix_fmt nv12 -s 1920x1080 -i 2019-06-03_19_14_25_018_1920x1080.yuv -vf crop=100:300:20:80 bbb.jpg
//ffplay -f rawvideo -pixel_format nv12 -video_size 1920x1080 tt.yuv
//ffplay -f rawvideo -pixel_format nv12 -video_size 1920x1080 -vf crop=100:100:100:0

你可能感兴趣的:(ffmpeg)