赞
踩
- /* messy code alarm */
- void video_frame_init(struct video_frame *frame, enum video_format format,
- uint32_t width, uint32_t height)
- {
- size_t size;
- size_t offsets[MAX_AV_PLANES];
- int alignment = base_get_alignment();
-
- if (!frame) return;
-
- memset(frame, 0, sizeof(struct video_frame));
- memset(offsets, 0, sizeof(offsets));
-
- switch (format) {
- case VIDEO_FORMAT_NONE:
- return;
-
- case VIDEO_FORMAT_I420:
- size = width * height;
- ALIGN_SIZE(size, alignment);
- offsets[0] = size;
- size += (width/2) * (height/2);
- ALIGN_SIZE(size, alignment);
- offsets[1] = size;
- size += (width/2) * (height/2);
- ALIGN_SIZE(size, alignment);
- frame->data[0] = bmalloc(size);
- frame->data[1] = (uint8_t*)frame->data[0] + offsets[0];
- frame->data[2] = (uint8_t*)frame->data[0] + offsets[1];
- frame->linesize[0] = width;
- frame->linesize[1] = width/2;
- frame->linesize[2] = width/2;
- break;
-
- case VIDEO_FORMAT_NV12:
- size = width * height;
- ALIGN_SIZE(size, alignment);
- offsets[0] = size;
- size += (width/2) * (height/2) * 2;
- ALIGN_SIZE(size, alignment);
- frame->data[0] = bmalloc(size);
- frame->data[1] = (uint8_t*)frame->data[0] + offsets[0];
- frame->linesize[0] = width;
- frame->linesize[1] = width;
- break;
-
- case VIDEO_FORMAT_Y800:
- size = width * height;
- ALIGN_SIZE(size, alignment);
- frame->data[0] = bmalloc(size);
- frame->linesize[0] = width;
- break;
-
- case VIDEO_FORMAT_YVYU:
- case VIDEO_FORMAT_YUY2:
- case VIDEO_FORMAT_UYVY:
- size = width * height * 2;
- ALIGN_SIZE(size, alignment);
- frame->data[0] = bmalloc(size);
- frame->linesize[0] = width*2;
- break;
-
- case VIDEO_FORMAT_RGBA:
- case VIDEO_FORMAT_BGRA:
- case VIDEO_FORMAT_BGRX:
- size = width * height * 4;
- ALIGN_SIZE(size, alignment);
- frame->data[0] = bmalloc(size);
- frame->linesize[0] = width*4;
- break;
-
- case VIDEO_FORMAT_I444:
- size = width * height;
- ALIGN_SIZE(size, alignment);
- frame->data[0] = bmalloc(size * 3);
- frame->data[1] = (uint8_t*)frame->data[0] + size;
- frame->data[2] = (uint8_t*)frame->data[1] + size;
- frame->linesize[0] = width;
- frame->linesize[1] = width;
- frame->linesize[2] = width;
- break;
- }
- }
- void video_frame_copy(struct video_frame *dst, const struct video_frame *src,
- enum video_format format, uint32_t cy)
- {
- switch (format) {
- case VIDEO_FORMAT_NONE:
- return;
-
- case VIDEO_FORMAT_I420:
- memcpy(dst->data[0], src->data[0], src->linesize[0] * cy);
- memcpy(dst->data[1], src->data[1], src->linesize[1] * cy / 2);
- memcpy(dst->data[2], src->data[2], src->linesize[2] * cy / 2);
- break;
-
- case VIDEO_FORMAT_NV12:
- memcpy(dst->data[0], src->data[0], src->linesize[0] * cy);
- memcpy(dst->data[1], src->data[1], src->linesize[1] * cy / 2);
- break;
-
- case VIDEO_FORMAT_Y800:
- case VIDEO_FORMAT_YVYU:
- case VIDEO_FORMAT_YUY2:
- case VIDEO_FORMAT_UYVY:
- case VIDEO_FORMAT_RGBA:
- case VIDEO_FORMAT_BGRA:
- case VIDEO_FORMAT_BGRX:
- memcpy(dst->data[0], src->data[0], src->linesize[0] * cy);
- break;
-
- case VIDEO_FORMAT_I444:
- memcpy(dst->data[0], src->data[0], src->linesize[0] * cy);
- memcpy(dst->data[1], src->data[1], src->linesize[1] * cy);
- memcpy(dst->data[2], src->data[2], src->linesize[2] * cy);
- break;
- }
- }
-
- void video_frame_setblack(struct video_frame *dst, enum video_format format, uint32_t cy)
- {
- switch (format) {
- case VIDEO_FORMAT_NONE:
- return;
-
- case VIDEO_FORMAT_I420:
- memset(dst->data[0], 0, dst->linesize[0] * cy);
- memset(dst->data[1], 0x80, dst->linesize[1] * cy / 2);
- memset(dst->data[2], 0x80, dst->linesize[2] * cy / 2);
- break;
-
- case VIDEO_FORMAT_NV12:
- memset(dst->data[0], 0x10, dst->linesize[0] * cy);
- memset(dst->data[1], 0x80, dst->linesize[1] * cy / 2);
- break;
-
- case VIDEO_FORMAT_Y800:
- case VIDEO_FORMAT_YVYU:
- case VIDEO_FORMAT_YUY2:
- case VIDEO_FORMAT_UYVY:
- case VIDEO_FORMAT_RGBA:
- case VIDEO_FORMAT_BGRA:
- case VIDEO_FORMAT_BGRX:
- memset(dst->data[0], 0, dst->linesize[0] * cy);
- break;
-
- case VIDEO_FORMAT_I444:
- memset(dst->data[0], 0, dst->linesize[0] * cy*3);
- // memset(dst->data[1], 0, dst->linesize[0] * cy);
- // memset(dst->data[2], 0, dst->linesize[0] * cy);
- break;
- }
- }
- void convert_nv12_to_uyvy(uint8_t* input[], uint32_t in_linesize[],
- uint32_t start_y, uint32_t end_y,
- uint8_t* output, uint32_t out_linesize)
- {
- register uint8_t* _Y;
- register uint8_t* _U;
- register uint8_t* _V;
- register uint8_t* _out;
- uint32_t width = min_uint32(in_linesize[0], out_linesize);
- uint8_t* _YL = input[0] + (start_y * in_linesize[0]);
- uint8_t* _UL = input[1] + ((start_y / 2) * in_linesize[1]);
-
- uint8_t* _outL = output + (start_y * out_linesize);
-
-
- for (uint32_t y = start_y; y < end_y; y++) {
- /*_Y = input[0] + (y * in_linesize[0]);
- _U = input[1] + ((y/2) * in_linesize[1]);
- _V = _U + 1;
- _out = output + (y * out_linesize);*/
- _Y = _YL;
- _U = _UL;
- _V = _U + 1;
-
- _out = _outL;
-
- for (uint32_t x = 0; x < width; x+=2) {
- *(_out++) = *(_U++); _U++;
- *(_out++) = *(_Y++);
- *(_out++) = *(_V++); _V++;
- *(_out++) = *(_Y++);
- }
-
- _YL += in_linesize[0];
- _UL += y&0x1?in_linesize[1]:0;
- _outL += out_linesize;
- }
- }
-
- void convert_i420_to_uyvy(uint8_t* input[], uint32_t in_linesize[],
- uint32_t start_y, uint32_t end_y,
- uint8_t* output, uint32_t out_linesize)
- {
- uint8_t* _Y;
- uint8_t* _U;
- uint8_t* _V;
- uint8_t* _out;
- uint32_t width = min_uint32(in_linesize[0], out_linesize);
- for (uint32_t y = start_y; y < end_y; y++) {
- _Y = input[0] + (y * in_linesize[0]);
- _U = input[1] + ((y/2) * in_linesize[1]);
- _V = input[2] + ((y/2) * in_linesize[2]);
-
- _out = output + (y * out_linesize);
-
- for (uint32_t x = 0; x < width; x += 2) {
- *(_out++) = *(_U++);
- *(_out++) = *(_Y++);
- *(_out++) = *(_V++);
- *(_out++) = *(_Y++);
- }
- }
- }
-
- void convert_i444_to_uyvy(uint8_t* input[], uint32_t in_linesize[],
- uint32_t start_y, uint32_t end_y,
- uint8_t* output, uint32_t out_linesize)
- {
- uint8_t* _Y;
- uint8_t* _U;
- uint8_t* _V;
- uint8_t* _out;
- uint32_t width = min_uint32(in_linesize[0], out_linesize);
- for (uint32_t y = start_y; y < end_y; y++) {
- _Y = input[0] + (y * in_linesize[0]);
- _U = input[1] + (y * in_linesize[1]);
- _V = input[2] + (y * in_linesize[2]);
-
- _out = output + (y * out_linesize);
-
- for (uint32_t x = 0; x < width; x += 2) {
- // Quality loss here. Some chroma samples are ignored.
- *(_out++) = *(_U++); _U++;
- *(_out++) = *(_Y++);
- *(_out++) = *(_V++); _V++;
- *(_out++) = *(_Y++);
- }
- }
- }
参考资料:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。