-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
655 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include "vpx_common.h" | ||
|
||
UNIFEX_TERM result_error( | ||
UnifexEnv *env, | ||
const char *reason, | ||
UNIFEX_TERM (*result_error_fun)(UnifexEnv *, const char *), | ||
vpx_codec_ctx_t *codec_context, | ||
void *state | ||
) { | ||
char *full_reason; | ||
if (codec_context) { | ||
const char *error = vpx_codec_error(codec_context); | ||
const char *detail = vpx_codec_error_detail(codec_context); | ||
if (detail) { | ||
full_reason = unifex_alloc(strlen(reason) + strlen(error) + strlen(detail) + 5); | ||
sprintf(full_reason, "%s: %s: %s", reason, error, detail); | ||
} else { | ||
full_reason = unifex_alloc(strlen(reason) + strlen(error) + 3); | ||
sprintf(full_reason, "%s: %s", reason, error); | ||
} | ||
} else { | ||
full_reason = unifex_alloc(strlen(reason) + 1); | ||
sprintf(full_reason, "%s", reason); | ||
} | ||
|
||
if (state) unifex_release_resource(state); | ||
|
||
UNIFEX_TERM result = result_error_fun(env, full_reason); | ||
unifex_free(full_reason); | ||
return result; | ||
} | ||
|
||
Dimensions get_plane_dimensions(const vpx_image_t *img, int plane) { | ||
const int height = | ||
(plane > 0 && img->y_chroma_shift > 0) ? (img->d_h + 1) >> img->y_chroma_shift : img->d_h; | ||
|
||
int width = | ||
(plane > 0 && img->x_chroma_shift > 0) ? (img->d_w + 1) >> img->x_chroma_shift : img->d_w; | ||
|
||
// Fixing NV12 chroma width if it is odd | ||
if (img->fmt == VPX_IMG_FMT_NV12 && plane == 1) width = (width + 1) & ~1; | ||
|
||
return (Dimensions){width, height}; | ||
} | ||
|
||
void convert_between_image_and_raw_frame( | ||
vpx_image_t *img, UnifexPayload *raw_frame, ConversionType conversion_type | ||
) { | ||
const int bytes_per_pixel = (img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1; | ||
|
||
// Assuming that for nv12 we write all chroma data at once | ||
const int number_of_planes = (img->fmt == VPX_IMG_FMT_NV12) ? 2 : 3; | ||
unsigned char *frame_data = raw_frame->data; | ||
|
||
for (int plane = 0; plane < number_of_planes; ++plane) { | ||
unsigned char *image_buf = img->planes[plane]; | ||
const int stride = img->stride[plane]; | ||
Dimensions plane_dimensions = get_plane_dimensions(img, plane); | ||
|
||
for (unsigned int y = 0; y < plane_dimensions.height; ++y) { | ||
size_t bytes_to_write = bytes_per_pixel * plane_dimensions.width; | ||
switch (conversion_type) { | ||
case RAW_FRAME_TO_IMAGE: | ||
memcpy(image_buf, frame_data, bytes_to_write); | ||
break; | ||
|
||
case IMAGE_TO_RAW_FRAME: | ||
memcpy(frame_data, image_buf, bytes_to_write); | ||
break; | ||
} | ||
image_buf += stride; | ||
frame_data += bytes_to_write; | ||
} | ||
} | ||
} | ||
|
||
void free_payloads(UnifexPayload **payloads, unsigned int payloads_cnt) { | ||
for (unsigned int i = 0; i < payloads_cnt; i++) { | ||
if (payloads[i] != NULL) { | ||
unifex_payload_release(payloads[i]); | ||
unifex_free(payloads[i]); | ||
} | ||
} | ||
unifex_free(payloads); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#pragma once | ||
#include "vpx/vpx_codec.h" | ||
#include "vpx/vpx_image.h" | ||
#include <unifex/payload.h> | ||
#include <unifex/unifex.h> | ||
|
||
typedef struct Dimensions { | ||
unsigned int width; | ||
unsigned int height; | ||
} Dimensions; | ||
|
||
UNIFEX_TERM result_error( | ||
UnifexEnv *env, | ||
const char *reason, | ||
UNIFEX_TERM (*result_error_fun)(UnifexEnv *, const char *), | ||
vpx_codec_ctx_t *codec_context, | ||
void *state | ||
); | ||
|
||
typedef enum ConversionType { IMAGE_TO_RAW_FRAME, RAW_FRAME_TO_IMAGE } ConversionType; | ||
|
||
Dimensions get_plane_dimensions(const vpx_image_t *img, int plane); | ||
|
||
void free_payloads(UnifexPayload **payloads, unsigned int payloads_cnt); | ||
|
||
void convert_between_image_and_raw_frame( | ||
vpx_image_t *img, UnifexPayload *raw_frame, ConversionType conversion_type | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,12 @@ | ||
#pragma once | ||
#include "vpx/vp8dx.h" | ||
#include "vpx/vpx_decoder.h" | ||
#include "vpx_common.h" | ||
#include <erl_nif.h> | ||
|
||
typedef struct State { | ||
vpx_codec_ctx_t codec_context; | ||
vpx_codec_iface_t *codec_interface; | ||
} State; | ||
|
||
typedef struct Dimensions { | ||
unsigned int width; | ||
unsigned int height; | ||
} Dimensions; | ||
|
||
#include "_generated/vpx_decoder.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.