ZLMediaKit/api/source/mk_transcode.cpp

194 lines
6.1 KiB
C++
Raw Normal View History

2022-05-25 15:38:32 +08:00
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#include "mk_transcode.h"
#include "Extension/Track.h"
using namespace mediakit;
2023-02-11 11:35:37 +08:00
std::vector<std::string> toCodecList(const char *codec_name_list[]) {
std::vector<std::string> codec_list;
auto i = 0U;
while (codec_name_list[i]) {
codec_list.emplace_back(codec_name_list[i]);
++i;
}
return codec_list;
}
2022-05-25 15:38:32 +08:00
#ifdef ENABLE_FFMPEG
#include "Codec/Transcode.h"
API_EXPORT mk_decoder API_CALL mk_decoder_create(mk_track track, int thread_num) {
assert(track);
2023-02-11 11:35:37 +08:00
return new FFmpegDecoder(*((Track::Ptr *) track), thread_num);
}
API_EXPORT mk_decoder API_CALL mk_decoder_create2(mk_track track, int thread_num, const char *codec_name_list[]) {
assert(track && codec_name_list);
return new FFmpegDecoder(*((Track::Ptr *) track), thread_num, toCodecList(codec_name_list));
2022-05-25 15:38:32 +08:00
}
API_EXPORT void API_CALL mk_decoder_release(mk_decoder ctx, int flush_frame) {
assert(ctx);
2023-02-11 11:35:37 +08:00
auto decoder = (FFmpegDecoder *) ctx;
2022-05-25 15:38:32 +08:00
if (flush_frame) {
decoder->stopThread(false);
}
delete decoder;
}
API_EXPORT void API_CALL mk_decoder_decode(mk_decoder ctx, mk_frame frame, int async, int enable_merge) {
assert(ctx && frame);
2023-02-11 11:35:37 +08:00
((FFmpegDecoder *) ctx)->inputFrame(*((Frame::Ptr *) frame), false, async, enable_merge);
2022-05-25 15:38:32 +08:00
}
API_EXPORT void API_CALL mk_decoder_set_max_async_frame_size(mk_decoder ctx, size_t size) {
assert(ctx && size);
2023-02-11 11:35:37 +08:00
((FFmpegDecoder *) ctx)->setMaxTaskSize(size);
2022-05-25 15:38:32 +08:00
}
API_EXPORT void API_CALL mk_decoder_set_cb(mk_decoder ctx, on_mk_decode cb, void *user_data) {
mk_decoder_set_cb2(ctx, cb, user_data, nullptr);
}
API_EXPORT void API_CALL mk_decoder_set_cb2(mk_decoder ctx, on_mk_decode cb, void *user_data, on_user_data_free user_data_free){
2022-05-25 15:38:32 +08:00
assert(ctx && cb);
std::shared_ptr<void> ptr(user_data, user_data_free ? user_data_free : [](void *) {});
((FFmpegDecoder *) ctx)->setOnDecode([cb, ptr](const FFmpegFrame::Ptr &pix_frame) {
cb(ptr.get(), (mk_frame_pix) &pix_frame);
2022-05-25 15:38:32 +08:00
});
}
API_EXPORT const AVCodecContext *API_CALL mk_decoder_get_context(mk_decoder ctx) {
assert(ctx);
2023-02-11 11:35:37 +08:00
return ((FFmpegDecoder *) ctx)->getContext();
2022-05-25 15:38:32 +08:00
}
/////////////////////////////////////////////////////////////////////////////////////////////
API_EXPORT mk_frame_pix API_CALL mk_frame_pix_ref(mk_frame_pix frame) {
assert(frame);
2023-02-11 11:35:37 +08:00
return new FFmpegFrame::Ptr(*(FFmpegFrame::Ptr *) frame);
2022-05-25 15:38:32 +08:00
}
API_EXPORT mk_frame_pix API_CALL mk_frame_pix_from_av_frame(AVFrame *frame) {
assert(frame);
2023-02-11 11:35:37 +08:00
return new FFmpegFrame::Ptr(std::make_shared<FFmpegFrame>(std::shared_ptr<AVFrame>(av_frame_clone(frame), [](AVFrame *frame){
av_frame_free(&frame);
})));
}
API_EXPORT mk_frame_pix API_CALL mk_frame_pix_from_buffer(mk_buffer plane_data[], int line_size[], int plane) {
assert(plane <= AV_NUM_DATA_POINTERS);
std::shared_ptr<AVFrame> frame(av_frame_alloc(), [](AVFrame *ptr) {
av_frame_free(&ptr);
});
std::vector<mk_buffer> buffer_array;
for (auto i = 0; i < plane; ++i) {
auto buffer = mk_buffer_ref(plane_data[i]);
frame->data[i] = (uint8_t *) mk_buffer_get_data(buffer);
frame->linesize[i] = line_size[i];
buffer_array.emplace_back(buffer);
}
return new FFmpegFrame::Ptr(new FFmpegFrame(std::move(frame)), [buffer_array](FFmpegFrame *frame) {
for (auto &buffer : buffer_array) {
mk_buffer_unref(buffer);
}
delete frame;
});
2022-05-25 15:38:32 +08:00
}
API_EXPORT void API_CALL mk_frame_pix_unref(mk_frame_pix frame) {
assert(frame);
2023-02-11 11:35:37 +08:00
delete (FFmpegFrame::Ptr *) frame;
2022-05-25 15:38:32 +08:00
}
API_EXPORT AVFrame *API_CALL mk_frame_pix_get_av_frame(mk_frame_pix frame) {
assert(frame);
2023-02-11 11:35:37 +08:00
return (*(FFmpegFrame::Ptr *) frame)->get();
2022-05-25 15:38:32 +08:00
}
2023-02-11 11:35:37 +08:00
/////////////////////////////////////////////////////////////////////////////////////////////
2022-05-25 15:38:32 +08:00
API_EXPORT mk_swscale mk_swscale_create(int output, int width, int height) {
2023-02-11 11:35:37 +08:00
return new FFmpegSws((AVPixelFormat) output, width, height);
2022-05-25 15:38:32 +08:00
}
API_EXPORT void mk_swscale_release(mk_swscale ctx) {
2023-02-11 11:35:37 +08:00
delete (FFmpegSws *) ctx;
2022-05-25 15:38:32 +08:00
}
API_EXPORT int mk_swscale_input_frame(mk_swscale ctx, mk_frame_pix frame, uint8_t *data) {
2023-02-11 11:35:37 +08:00
return ((FFmpegSws *) ctx)->inputFrame(*(FFmpegFrame::Ptr *) frame, data);
2022-05-25 15:38:32 +08:00
}
2023-02-11 11:35:37 +08:00
API_EXPORT mk_frame_pix mk_swscale_input_frame2(mk_swscale ctx, mk_frame_pix frame){
return new FFmpegFrame::Ptr(((FFmpegSws *) ctx)->inputFrame(*(FFmpegFrame::Ptr *) frame));
2022-05-25 15:38:32 +08:00
}
API_EXPORT uint8_t **API_CALL mk_get_av_frame_data(AVFrame *frame) {
return frame->data;
}
2023-02-11 11:35:37 +08:00
API_EXPORT void API_CALL mk_set_av_frame_data(AVFrame *frame, uint8_t *data, int plane) {
frame->data[plane] = data;
}
2022-05-25 15:38:32 +08:00
API_EXPORT int *API_CALL mk_get_av_frame_line_size(AVFrame *frame) {
return frame->linesize;
}
2023-02-11 11:35:37 +08:00
API_EXPORT void API_CALL mk_set_av_frame_line_size(AVFrame *frame, int line_size, int plane) {
frame->linesize[plane] = line_size;
}
API_EXPORT int64_t API_CALL mk_get_av_frame_dts(AVFrame *frame) {
2022-05-25 15:38:32 +08:00
return frame->pkt_dts;
}
2023-02-11 11:35:37 +08:00
API_EXPORT void API_CALL mk_set_av_frame_dts(AVFrame *frame, int64_t dts) {
frame->pkt_dts = dts;
}
API_EXPORT int64_t API_CALL mk_get_av_frame_pts(AVFrame *frame) {
2022-05-25 15:38:32 +08:00
return frame->pts;
}
2023-02-11 11:35:37 +08:00
API_EXPORT void API_CALL mk_set_av_frame_pts(AVFrame *frame, int64_t pts) {
frame->pts = pts;
}
2022-05-25 15:38:32 +08:00
API_EXPORT int API_CALL mk_get_av_frame_width(AVFrame *frame) {
return frame->width;
}
2023-02-11 11:35:37 +08:00
API_EXPORT void API_CALL mk_set_av_frame_width(AVFrame *frame, int width) {
frame->width = width;
}
2022-05-25 15:38:32 +08:00
API_EXPORT int API_CALL mk_get_av_frame_height(AVFrame *frame) {
return frame->height;
}
2023-02-11 11:35:37 +08:00
API_EXPORT void API_CALL mk_set_av_frame_height(AVFrame *frame, int height) {
frame->height = height;
}
2022-05-25 15:38:32 +08:00
API_EXPORT int API_CALL mk_get_av_frame_format(AVFrame *frame) {
return frame->format;
}
2023-02-11 11:35:37 +08:00
API_EXPORT void API_CALL mk_set_av_frame_format(AVFrame *frame, int format) {
frame->format = format;
}
2022-05-25 15:38:32 +08:00
#endif //ENABLE_FFMPEG