-
Notifications
You must be signed in to change notification settings - Fork 4
Add video converter #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this fails, we need to free video_converter. I can see that I implemented audio the same way but I think that's a bug
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, maybe that's not a bug as in our case, decoder's destructor will do the thing but I think we should fix this anyway i.e. free converter and leave xav_decoder->vc untouched.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes the destructor will free the converter in case of error.