static void av_unused avframe2img(AVFrame *pict, switch_image_t *img)
{
- int i;
- uint8_t *y = pict->data[0];
- uint8_t *u = pict->data[1];
- uint8_t *v = pict->data[2];
+ int i, j;
- /* Y */
- for (i = 0; i < img->d_h; i++) {
- memcpy(&img->planes[0][i * img->stride[0]], y + i * pict->linesize[0], img->d_w);
- }
+ if (img->fmt == SWITCH_IMG_FMT_I420) {
+ if (pict->format == AV_PIX_FMT_YUV420P) {
+ switch_I420_copy2(pict->data, pict->linesize, img->planes, img->stride, img->d_w, img->d_h);
+ } else if (pict->format == AV_PIX_FMT_YUVA420P) {
+ int linesize[3];
+ linesize[0] = pict->linesize[0];
+ linesize[1] = pict->linesize[1];
+ linesize[2] = pict->linesize[2] + pict->linesize[0];
- /* U/V */
- for(i = 0; i < pict->height / 2; i++) {
- memcpy(&img->planes[1][i * img->stride[1]], u + i * pict->linesize[1], img->d_w / 2);
- memcpy(&img->planes[2][i * img->stride[2]], v + i * pict->linesize[2], img->d_w / 2);
+ switch_I420_copy2(pict->data, linesize, img->planes, img->stride, img->d_w, img->d_h);
+ }
+
+ } else if (img->fmt == SWITCH_IMG_FMT_ARGB) {
+ if (pict->format == AV_PIX_FMT_YUV420P) {
+ switch_rgb_color_t *color = (switch_rgb_color_t *)img->planes[SWITCH_PLANE_PACKED];
+ uint8_t *alpha = pict->data[3];
+
+ /*!\brief I420 to ARGB Convertion*/
+ switch_I420ToARGB(pict->data[0], pict->linesize[0],
+ pict->data[1], pict->linesize[1],
+ pict->data[2], pict->linesize[2],
+ img->planes[SWITCH_PLANE_PACKED], img->stride[SWITCH_PLANE_PACKED],
+ img->d_w, img->d_h);
+
+
+ for (j = 0; j < img->d_h; j++) {
+ for (i = 0; i < img->d_w; i++) {
+ color->a = *alpha++;
+ color++;
+ }
+ color = (switch_rgb_color_t *)(img->planes[SWITCH_PLANE_PACKED] + img->stride[SWITCH_PLANE_PACKED] * j);
+ }
+ } else if (pict->format == AV_PIX_FMT_RGBA) {
+#if SWITCH_BYTE_ORDER == __BIG_ENDIAN
+ switch_RGBAToARGB(pict->data[0], pict->linesize[0],
+ mg->planes[SWITCH_PLANE_PACKED], img->stride[SWITCH_PLANE_PACKED],
+ img->d_w, img->d_h);
+#else
+ switch_ABGRToARGB(pict->data[0], pict->linesize[0],
+ img->planes[SWITCH_PLANE_PACKED], img->stride[SWITCH_PLANE_PACKED],
+ img->d_w, img->d_h);
+#endif
+ } else if (pict->format == AV_PIX_FMT_BGRA) {
+#if SWITCH_BYTE_ORDER == __BIG_ENDIAN
+ switch_BGRAToARGB(pict->data[0], pict->linesize[0],
+ , img->planes[SWITCH_PLANE_PACKED], img->stride[SWITCH_PLANE_PACKED],
+ , img->d_w, img->d_h);
+#else
+ switch_ARGBToARGB(pict->data[0], pict->linesize[0],
+ img->planes[SWITCH_PLANE_PACKED], img->stride[SWITCH_PLANE_PACKED],
+ img->d_w, img->d_h);
+#endif
+ }
}
}
switch_bool_t read_paused;
int errs;
switch_file_handle_t *handle;
+ switch_bool_t alpha_mode;
};
typedef struct av_file_context av_file_context_t;
{
AVCodec *audio_codec = NULL;
AVCodec *video_codec = NULL;
+ AVDictionary *opts = NULL;
int error;
int i;
switch_status_t status = SWITCH_STATUS_SUCCESS;
+ // av_dict_set(&opts, "c:v", "libvpx", 0);
+
/** Open the input file to read from it. */
- if ((error = avformat_open_input(&context->fc, filename, NULL,
- NULL)) < 0) {
+ if ((error = avformat_open_input(&context->fc, filename, NULL, NULL)) < 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not open input file '%s' (error '%s')\n", filename, get_error_text(error));
switch_goto_status(SWITCH_STATUS_FALSE, err);
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "file %s is %sseekable\n", filename, handle->seekable ? "" : "not ");
/** Get information on the input file (number of streams etc.). */
- if ((error = avformat_find_stream_info(context->fc, NULL)) < 0) {
+ if ((error = avformat_find_stream_info(context->fc, opts ? &opts : NULL)) < 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not open find stream info (error '%s')\n", get_error_text(error));
+ if (opts) av_dict_free(&opts);
switch_goto_status(SWITCH_STATUS_FALSE, err);
}
+ if (opts) av_dict_free(&opts);
+
av_dump_format(context->fc, 0, filename, 0);
for (i = 0; i< context->fc->nb_streams; i++) {
// av_frame_free(&vframe);
// continue;
//}
+ // switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "got_data=%d, error=%d\n", got_data, error);
if (got_data && error >= 0) {
- // switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "got picture %dx%d fmt: %d pktpts:%lld pktdts:%lld\n", vframe->width, vframe->height, vframe->format, vframe->pkt_pts, vframe->pkt_dts);
-
- if (vframe->format != AV_PIX_FMT_YUV420P) {
+ switch_img_fmt_t fmt = SWITCH_IMG_FMT_I420;
+ if (context->alpha_mode && (
+ vframe->format == AV_PIX_FMT_YUVA420P ||
+ vframe->format == AV_PIX_FMT_RGBA ||
+ vframe->format == AV_PIX_FMT_ARGB ||
+ vframe->format == AV_PIX_FMT_BGRA )) {
+ fmt = SWITCH_IMG_FMT_ARGB;
+ } else if (vframe->format != AV_PIX_FMT_YUV420P) {
AVFrame *frm = vframe;
int ret;
}
}
- img = switch_img_alloc(NULL, SWITCH_IMG_FMT_I420, vframe->width, vframe->height, 1);
+ img = switch_img_alloc(NULL, fmt, vframe->width, vframe->height, 1);
if (img) {
int64_t *pts = malloc(sizeof(int64_t));
}
}
}
+
av_frame_free(&vframe);
if (eof) {
context->offset = DFT_RECORD_OFFSET;
context->handle = handle;
- if (handle->params && (tmp = switch_event_get_header(handle->params, "av_video_offset"))) {
- context->offset = atoi(tmp);
+ if (handle->params) {
+ if ((tmp = switch_event_get_header(handle->params, "av_video_offset"))) {
+ context->offset = atoi(tmp);
+ } else if ((tmp = switch_event_get_header(handle->params, "alpha_mode"))) {
+ context->alpha_mode = switch_true(tmp);
+ }
}
switch_mutex_init(&context->mutex, SWITCH_MUTEX_NESTED, handle->memory_pool);
img->user_priv = (void *)(intptr_t)1;
- ARGBAttenuate(img->planes[SWITCH_PLANE_PACKED], img->stride[SWITCH_PLANE_PACKED],
+ ARGBAttenuate(img->planes[SWITCH_PLANE_PACKED], img->stride[SWITCH_PLANE_PACKED],
img->planes[SWITCH_PLANE_PACKED], img->stride[SWITCH_PLANE_PACKED], img->d_w, img->d_h);
}
SWITCH_DECLARE(void) switch_img_copy(switch_image_t *img, switch_image_t **new_img)
{
+#ifdef SWITCH_HAVE_YUV
+ switch_img_fmt_t new_fmt = img->fmt;
+
switch_assert(img);
switch_assert(new_img);
-#ifdef SWITCH_HAVE_YUV
if (img->fmt != SWITCH_IMG_FMT_I420 && img->fmt != SWITCH_IMG_FMT_ARGB) return;
- if (*new_img != NULL) {
- if (img->fmt != (*new_img)->fmt || img->d_w != (*new_img)->d_w || img->d_h != (*new_img)->d_w) {
+ if (*new_img) {
+ if ((*new_img)->fmt != SWITCH_IMG_FMT_I420 && (*new_img)->fmt != SWITCH_IMG_FMT_ARGB) return;
+ if (img->d_w != (*new_img)->d_w || img->d_h != (*new_img)->d_w ) {
+ new_fmt = (*new_img)->fmt;
switch_img_free(new_img);
}
}
if (*new_img == NULL) {
- *new_img = switch_img_alloc(NULL, img->fmt, img->d_w, img->d_h, 1);
+ *new_img = switch_img_alloc(NULL, new_fmt, img->d_w, img->d_h, 1);
}
switch_assert(*new_img);
if (img->fmt == SWITCH_IMG_FMT_I420) {
- I420Copy(img->planes[SWITCH_PLANE_Y], img->stride[SWITCH_PLANE_Y],
- img->planes[SWITCH_PLANE_U], img->stride[SWITCH_PLANE_U],
- img->planes[SWITCH_PLANE_V], img->stride[SWITCH_PLANE_V],
- (*new_img)->planes[SWITCH_PLANE_Y], (*new_img)->stride[SWITCH_PLANE_Y],
- (*new_img)->planes[SWITCH_PLANE_U], (*new_img)->stride[SWITCH_PLANE_U],
- (*new_img)->planes[SWITCH_PLANE_V], (*new_img)->stride[SWITCH_PLANE_V],
- img->d_w, img->d_h);
+ if (new_fmt == SWITCH_IMG_FMT_I420) {
+ I420Copy(img->planes[SWITCH_PLANE_Y], img->stride[SWITCH_PLANE_Y],
+ img->planes[SWITCH_PLANE_U], img->stride[SWITCH_PLANE_U],
+ img->planes[SWITCH_PLANE_V], img->stride[SWITCH_PLANE_V],
+ (*new_img)->planes[SWITCH_PLANE_Y], (*new_img)->stride[SWITCH_PLANE_Y],
+ (*new_img)->planes[SWITCH_PLANE_U], (*new_img)->stride[SWITCH_PLANE_U],
+ (*new_img)->planes[SWITCH_PLANE_V], (*new_img)->stride[SWITCH_PLANE_V],
+ img->d_w, img->d_h);
+ } else if (new_fmt == SWITCH_IMG_FMT_ARGB) {
+ I420ToARGB(img->planes[SWITCH_PLANE_Y], img->stride[SWITCH_PLANE_Y],
+ img->planes[SWITCH_PLANE_U], img->stride[SWITCH_PLANE_U],
+ img->planes[SWITCH_PLANE_V], img->stride[SWITCH_PLANE_V],
+ (*new_img)->planes[SWITCH_PLANE_PACKED], (*new_img)->stride[SWITCH_PLANE_PACKED],
+ img->d_w, img->d_h);
+ }
} else if (img->fmt == SWITCH_IMG_FMT_ARGB) {
- ARGBCopy(img->planes[SWITCH_PLANE_PACKED], img->stride[SWITCH_PLANE_PACKED],
+ if (new_fmt == SWITCH_IMG_FMT_ARGB) {
+ ARGBCopy(img->planes[SWITCH_PLANE_PACKED], img->stride[SWITCH_PLANE_PACKED],
(*new_img)->planes[SWITCH_PLANE_PACKED], (*new_img)->stride[SWITCH_PLANE_PACKED],
img->d_w, img->d_h);
+ } else if (new_fmt == SWITCH_IMG_FMT_I420) {
+ ARGBToI420(img->planes[SWITCH_PLANE_PACKED], img->stride[SWITCH_PLANE_PACKED],
+ (*new_img)->planes[SWITCH_PLANE_Y], (*new_img)->stride[SWITCH_PLANE_Y],
+ (*new_img)->planes[SWITCH_PLANE_U], (*new_img)->stride[SWITCH_PLANE_U],
+ (*new_img)->planes[SWITCH_PLANE_V], (*new_img)->stride[SWITCH_PLANE_V],
+ img->d_w, img->d_h);
+ }
}
#else
return;
static inline void switch_core_rgb2lab(switch_rgb_color_t *rgb, switch_lab_color_t *lab)
{
- double x,y,z;
+ double x,y,z;
double r = rgb->r;
double g = rgb->g;
double b = rgb->b;
uint32_t gg;
uint32_t bb;
uint32_t color_count;
-
+
switch_rgb_color_t auto_color;
int no_cache;
int frames_read;
return SWITCH_STATUS_SUCCESS;
}
-SWITCH_DECLARE(switch_status_t) switch_chromakey_add_color(switch_chromakey_t *ck, switch_rgb_color_t *color, uint32_t threshold)
+SWITCH_DECLARE(switch_status_t) switch_chromakey_add_color(switch_chromakey_t *ck, switch_rgb_color_t *color, uint32_t threshold)
{
switch_assert(ck);
static inline int switch_color_dom_cmp(switch_rgb_color_t *c1, switch_rgb_color_t *c2)
{
-
+
int c1_max = get_max(c1);
int c2_max = get_max(c2);
-
+
if (c1_max && c1_max == c2_max) return 1;
return 0;
static inline void get_dom(switch_shade_t autocolor, switch_rgb_color_t *color, int *domP, int *aP, int *bP)
{
int dom, a, b;
-
+
switch(autocolor) {
case SWITCH_SHADE_RED:
dom = color->r;
#ifdef DEBUG_CHROMA
total_pixel++;
#endif
-
+
if (!ck->no_cache && cache_img && cache_pixel) {
switch_rgb_color_t *cache_color = (switch_rgb_color_t *)cache_pixel;
if (dom > a && dom > b && dom > tol) {
if (dom - a > a_tol && dom - b > b_tol) {
hits = 1;
- }
+ }
}
}
}
checked++;
#endif
}
-
+
end:
if (same > 100 && last_color && switch_color_dom_cmp(color, last_color)) {
png_bytep buffer = NULL;
switch_status_t status = SWITCH_STATUS_SUCCESS;
- buffer = malloc(img->d_w * img->d_h * 3);
- switch_assert(buffer);
+ if (img->fmt == SWITCH_IMG_FMT_I420) {
+ png.format = PNG_FORMAT_RGB;
+ buffer = malloc(img->d_w * img->d_h * 3);
+ switch_assert(buffer);
- I420ToRAW( img->planes[SWITCH_PLANE_Y], img->stride[SWITCH_PLANE_Y],
+ I420ToRAW(img->planes[SWITCH_PLANE_Y], img->stride[SWITCH_PLANE_Y],
img->planes[SWITCH_PLANE_U], img->stride[SWITCH_PLANE_U],
img->planes[SWITCH_PLANE_V], img->stride[SWITCH_PLANE_V],
buffer, img->d_w * 3,
img->d_w, img->d_h);
+ } else if (img->fmt == SWITCH_IMG_FMT_ARGB) {
+#if SWITCH_BYTE_ORDER == __BIG_ENDIAN
+ png.format = PNG_FORMAT_ARGB;
+#else
+ png.format = PNG_FORMAT_BGRA;
+#endif
+ buffer = img->planes[SWITCH_PLANE_PACKED];
+ }
png.version = PNG_IMAGE_VERSION;
- png.format = PNG_FORMAT_RGB;
png.width = img->d_w;
png.height = img->d_h;
status = SWITCH_STATUS_FALSE;
}
- switch_safe_free(buffer);
+ if (img->fmt == SWITCH_IMG_FMT_I420) {
+ free(buffer);
+ }
+
return status;
}
uint32_t fourcc;
int ret;
- switch_assert(src->fmt == SWITCH_IMG_FMT_I420); // todo: support other formats
switch_assert(dest);
fourcc = switch_img_fmt2fourcc(fmt);
return SWITCH_STATUS_FALSE;
}
- ret = ConvertFromI420(src->planes[0], src->stride[0],
- src->planes[1], src->stride[1],
- src->planes[2], src->stride[2],
- dest, stride,
- src->d_w, src->d_h,
- fourcc);
+ if (src->fmt == SWITCH_IMG_FMT_I420) {
+ ret = ConvertFromI420(src->planes[0], src->stride[0],
+ src->planes[1], src->stride[1],
+ src->planes[2], src->stride[2],
+ dest, stride,
+ src->d_w, src->d_h,
+ fourcc);
+ } else if (src->fmt == SWITCH_IMG_FMT_ARGB && fmt == src->fmt) {
+ ret = ARGBCopy(src->planes[SWITCH_PLANE_PACKED], src->stride[SWITCH_PLANE_PACKED],
+ dest, stride,
+ src->d_w, src->d_h);
+ } else {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Convertion not supported %d -> %d\n", src->fmt, fmt);
+ return SWITCH_STATUS_FALSE;
+ }
return ret == 0 ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
#else
{
#ifdef SWITCH_HAVE_YUV
uint32_t fourcc;
- int ret;
+ int ret = -1;
fourcc = switch_img_fmt2fourcc(fmt);
src_size is only used when FOURCC_MJPG which we don't support so always 0
*/
- ret = ConvertToI420(src, 0,
+ if (dest->fmt == SWITCH_IMG_FMT_I420) {
+ ret = ConvertToI420(src, 0,
dest->planes[0], dest->stride[0],
dest->planes[1], dest->stride[1],
dest->planes[2], dest->stride[2],
width, height,
width, height,
0, fourcc);
+ } else if (dest->fmt == SWITCH_IMG_FMT_ARGB) {
+ ConvertToARGB(src, 0,
+ dest->planes[0], width * 4,
+ 0, 0,
+ width, height,
+ width, height,
+ 0, fourcc);
+ }
return ret == 0 ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
#else
dest = *destP;
}
- if (!dest) dest = switch_img_alloc(NULL, src->fmt, width, height, 1);
+ if (dest && src->fmt != dest->fmt) switch_img_free(&dest);
- switch_assert(src->fmt == dest->fmt);
+ if (!dest) dest = switch_img_alloc(NULL, src->fmt, width, height, 1);
if (src->fmt == SWITCH_IMG_FMT_I420) {
ret = I420Scale(src->planes[0], src->stride[0],
return SWITCH_STATUS_FALSE;
#endif
}
+
+SWITCH_DECLARE(switch_status_t) switch_I420ToARGB(const uint8_t *src_y, int src_stride_y,
+ const uint8_t *src_u, int src_stride_u,
+ const uint8_t *src_v, int src_stride_v,
+ uint8_t *dst_argb, int dst_stride_argb,
+ int width, int height)
+{
+
+#ifdef SWITCH_HAVE_YUV
+ int ret = I420ToARGB(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
+ dst_argb, dst_stride_argb, width, height);
+
+ return ret == 0 ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
+#else
+ return SWITCH_STATUS_FALSE;
+#endif
+}
+
+
+SWITCH_DECLARE(switch_status_t) switch_RGBAToARGB(const uint8_t* src_frame, int src_stride_frame,
+ uint8_t* dst_argb, int dst_stride_argb,
+ int width, int height)
+{
+#ifdef SWITCH_HAVE_YUV
+ int ret = RGBAToARGB(src_frame, src_stride_frame, dst_argb, dst_stride_argb, width, height);
+
+ return ret == 0 ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
+#else
+ return SWITCH_STATUS_FALSE;
+#endif
+}
+
+
+SWITCH_DECLARE(switch_status_t) switch_ABGRToARGB(const uint8_t* src_frame, int src_stride_frame,
+ uint8_t* dst_argb, int dst_stride_argb,
+ int width, int height)
+{
+#ifdef SWITCH_HAVE_YUV
+ int ret = ABGRToARGB(src_frame, src_stride_frame, dst_argb, dst_stride_argb, width, height);
+
+ return ret == 0 ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
+#else
+ return SWITCH_STATUS_FALSE;
+#endif
+}
+
+SWITCH_DECLARE(switch_status_t) switch_ARGBToARGB(const uint8_t* src_frame, int src_stride_frame,
+ uint8_t* dst_argb, int dst_stride_argb,
+ int width, int height)
+{
+#ifdef SWITCH_HAVE_YUV
+ int ret = ARGBToARGB(src_frame, src_stride_frame, dst_argb, dst_stride_argb, width, height);
+
+ return ret == 0 ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
+#else
+ return SWITCH_STATUS_FALSE;
+#endif
+}
+
+
/* For Emacs:
* Local Variables:
* mode:c