-
Notifications
You must be signed in to change notification settings - Fork 5
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
17 changed files
with
771 additions
and
43 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
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,43 +1,82 @@ | ||
#include "video_converter.h" | ||
#include "utils.h" | ||
|
||
int video_converter_convert(AVFrame *src_frame, AVFrame **dst_frame, | ||
enum AVPixelFormat out_format) { | ||
int ret; | ||
static inline unsigned int video_converter_resolution_changed(struct VideoConverter *converter, AVFrame *frame) { | ||
return converter->in_format != frame->format || | ||
converter->in_width != frame->width || | ||
converter->in_height != frame->height; | ||
} | ||
|
||
*dst_frame = av_frame_alloc(); | ||
if (!*dst_frame) { | ||
return -1; | ||
struct VideoConverter *video_converter_alloc() { | ||
struct VideoConverter *converter = | ||
(struct VideoConverter *)XAV_ALLOC(sizeof(struct VideoConverter)); | ||
if(converter) { | ||
converter->sws_ctx = NULL; | ||
converter->dst_frame = av_frame_alloc(); | ||
} | ||
return converter; | ||
} | ||
|
||
int video_converter_init(struct VideoConverter *converter, int in_width, int in_height, | ||
enum AVPixelFormat in_format, enum AVPixelFormat out_format) { | ||
converter->in_width = in_width; | ||
converter->in_height = in_height; | ||
converter->in_format = in_format; | ||
converter->out_format = out_format; | ||
|
||
av_frame_unref(converter->dst_frame); | ||
|
||
(*dst_frame)->width = src_frame->width; | ||
(*dst_frame)->height = src_frame->height; | ||
(*dst_frame)->format = out_format; | ||
(*dst_frame)->pts = src_frame->pts; | ||
converter->dst_frame->width = in_width; | ||
converter->dst_frame->height = in_height; | ||
converter->dst_frame->format = out_format; | ||
|
||
ret = av_frame_get_buffer(*dst_frame, 0); | ||
if (ret < 0) { | ||
int ret = av_frame_get_buffer(converter->dst_frame, 0); | ||
if (ret < 0) | ||
return ret; | ||
|
||
converter->sws_ctx = sws_getContext(in_width, in_height, in_format, in_width, in_height, out_format, | ||
SWS_BILINEAR, NULL, NULL, NULL); | ||
|
||
if (!converter->sws_ctx) { | ||
XAV_LOG_DEBUG("Couldn't get sws context"); | ||
return -1; | ||
} | ||
|
||
struct SwsContext *sws_ctx = | ||
sws_getContext(src_frame->width, src_frame->height, src_frame->format, src_frame->width, | ||
src_frame->height, out_format, SWS_BILINEAR, NULL, NULL, NULL); | ||
return 0; | ||
} | ||
|
||
if (ret < 0) { | ||
return ret; | ||
int video_converter_convert(struct VideoConverter *converter, AVFrame *src_frame) { | ||
int ret; | ||
|
||
if (video_converter_resolution_changed(converter, src_frame)) { | ||
XAV_LOG_DEBUG("Frame resolution changed"); | ||
sws_freeContext(converter->sws_ctx); | ||
ret = video_converter_init(converter, src_frame->width, src_frame->height, | ||
src_frame->format, converter->out_format); | ||
if (ret < 0) { | ||
return ret; | ||
} | ||
} | ||
|
||
converter->dst_frame->pts = src_frame->pts; | ||
|
||
// is this (const uint8_t * const*) cast really correct? | ||
ret = sws_scale(sws_ctx, (const uint8_t *const *)src_frame->data, src_frame->linesize, 0, | ||
src_frame->height, (*dst_frame)->data, (*dst_frame)->linesize); | ||
return sws_scale(converter->sws_ctx, (const uint8_t *const *)src_frame->data, src_frame->linesize, 0, | ||
src_frame->height, converter->dst_frame->data, converter->dst_frame->linesize); | ||
} | ||
|
||
if (ret < 0) { | ||
av_frame_free(dst_frame); | ||
sws_freeContext(sws_ctx); | ||
return ret; | ||
} | ||
void video_converter_free(struct VideoConverter **converter) { | ||
struct VideoConverter* vc = *converter; | ||
if (vc != NULL) { | ||
if (vc->sws_ctx != NULL) { | ||
sws_freeContext((*converter)->sws_ctx); | ||
} | ||
|
||
sws_freeContext(sws_ctx); | ||
if (vc->dst_frame != NULL) { | ||
av_frame_free(&(*converter)->dst_frame); | ||
} | ||
|
||
return ret; | ||
XAV_FREE(vc); | ||
*converter = NULL; | ||
} | ||
} |
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
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
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.