#define _WIN32_WINNT 0x0602
#endif
+#include "encode.h"
#include "mf_utils.h"
#include "libavutil/imgutils.h"
#include "libavutil/opt.h"
typedef struct MFContext {
AVClass *av_class;
+ AVFrame *frame;
int is_video, is_audio;
GUID main_subtype;
IMFTransform *mft;
if (FAILED(hr))
return AVERROR_EXTERNAL;
- if ((ret = av_new_packet(avpkt, len)) < 0)
+ if ((ret = ff_get_encode_buffer(avctx, avpkt, len, 0)) < 0)
return ret;
IMFSample_ConvertToContiguousBuffer(sample, &buffer);
return 0;
}
-static int mf_send_frame(AVCodecContext *avctx, const AVFrame *frame)
-{
- MFContext *c = avctx->priv_data;
- int ret;
- IMFSample *sample = NULL;
- if (frame) {
- sample = mf_avframe_to_sample(avctx, frame);
- if (!sample)
- return AVERROR(ENOMEM);
- if (c->is_video && c->codec_api) {
- if (frame->pict_type == AV_PICTURE_TYPE_I || !c->sample_sent)
- ICodecAPI_SetValue(c->codec_api, &ff_CODECAPI_AVEncVideoForceKeyFrame, FF_VAL_VT_UI4(1));
- }
- }
- ret = mf_send_sample(avctx, sample);
- if (sample)
- IMFSample_Release(sample);
- return ret;
-}
-
static int mf_receive_sample(AVCodecContext *avctx, IMFSample **out_sample)
{
MFContext *c = avctx->priv_data;
static int mf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
{
- IMFSample *sample;
+ MFContext *c = avctx->priv_data;
+ IMFSample *sample = NULL;
int ret;
+ if (!c->frame->buf[0]) {
+ ret = ff_encode_get_frame(avctx, c->frame);
+ if (ret < 0 && ret != AVERROR_EOF)
+ return ret;
+ }
+
+ if (c->frame->buf[0]) {
+ sample = mf_avframe_to_sample(avctx, c->frame);
+ if (!sample) {
+ av_frame_unref(c->frame);
+ return AVERROR(ENOMEM);
+ }
+ if (c->is_video && c->codec_api) {
+ if (c->frame->pict_type == AV_PICTURE_TYPE_I || !c->sample_sent)
+ ICodecAPI_SetValue(c->codec_api, &ff_CODECAPI_AVEncVideoForceKeyFrame, FF_VAL_VT_UI4(1));
+ }
+ }
+
+ ret = mf_send_sample(avctx, sample);
+ if (sample)
+ IMFSample_Release(sample);
+ if (ret != AVERROR(EAGAIN))
+ av_frame_unref(c->frame);
+ if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
+ return ret;
+
ret = mf_receive_sample(avctx, &sample);
if (ret < 0)
return ret;
const CLSID *subtype = ff_codec_to_mf_subtype(avctx->codec_id);
int use_hw = 0;
+ c->frame = av_frame_alloc();
+ if (!c->frame)
+ return AVERROR(ENOMEM);
+
c->is_audio = avctx->codec_type == AVMEDIA_TYPE_AUDIO;
c->is_video = !c->is_audio;
c->reorder_delay = AV_NOPTS_VALUE;
ff_free_mf(&c->mft);
+ av_frame_free(&c->frame);
+
av_freep(&avctx->extradata);
avctx->extradata_size = 0;
.option = OPTS, \
.version = LIBAVUTIL_VERSION_INT, \
}; \
- AVCodec ff_ ## NAME ## _mf_encoder = { \
+ const AVCodec ff_ ## NAME ## _mf_encoder = { \
.priv_class = &ff_ ## NAME ## _mf_encoder_class, \
.name = #NAME "_mf", \
.long_name = NULL_IF_CONFIG_SMALL(#ID " via MediaFoundation"), \
.priv_data_size = sizeof(MFContext), \
.init = mf_init, \
.close = mf_close, \
- .send_frame = mf_send_frame, \
.receive_packet = mf_receive_packet, \
EXTRA \
- .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HYBRID, \
+ .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HYBRID | \
+ AV_CODEC_CAP_DR1, \
.caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | \
FF_CODEC_CAP_INIT_CLEANUP, \
};