Lines Matching refs:avctx

59 static int mf_choose_output_type(AVCodecContext *avctx);
60 static int mf_setup_context(AVCodecContext *avctx);
66 static int mf_wait_events(AVCodecContext *avctx)
68 MFContext *c = avctx->priv_data;
78 av_log(avctx, AV_LOG_ERROR, "IMFMediaEventGenerator_GetEvent() failed: %s\n",
105 static AVRational mf_get_tb(AVCodecContext *avctx)
107 if (avctx->time_base.num > 0 && avctx->time_base.den > 0)
108 return avctx->time_base;
112 static LONGLONG mf_to_mf_time(AVCodecContext *avctx, int64_t av_pts)
116 return av_rescale_q(av_pts, mf_get_tb(avctx), MF_TIMEBASE);
119 static void mf_sample_set_pts(AVCodecContext *avctx, IMFSample *sample, int64_t av_pts)
121 LONGLONG stime = mf_to_mf_time(avctx, av_pts);
126 static int64_t mf_from_mf_time(AVCodecContext *avctx, LONGLONG stime)
128 return av_rescale_q(stime, MF_TIMEBASE, mf_get_tb(avctx));
131 static int64_t mf_sample_get_pts(AVCodecContext *avctx, IMFSample *sample)
137 return mf_from_mf_time(avctx, pts);
140 static int mf_enca_output_type_get(AVCodecContext *avctx, IMFMediaType *type)
142 MFContext *c = avctx->priv_data;
146 if (avctx->codec_id != AV_CODEC_ID_MP3 && avctx->codec_id != AV_CODEC_ID_AC3) {
149 avctx->extradata = av_mallocz(sz + AV_INPUT_BUFFER_PADDING_SIZE);
150 if (!avctx->extradata)
152 avctx->extradata_size = sz;
153 hr = IMFAttributes_GetBlob(type, &MF_MT_USER_DATA, avctx->extradata, sz, NULL);
157 if (avctx->codec_id == AV_CODEC_ID_AAC && avctx->extradata_size >= 12) {
159 avctx->extradata_size = avctx->extradata_size - 12;
160 memmove(avctx->extradata, avctx->extradata + 12, avctx->extradata_size);
171 av_log(avctx, AV_LOG_VERBOSE, "MFT_OUTPUT_STREAM_INFO.cbSize set to 0, "
180 static int mf_encv_output_type_get(AVCodecContext *avctx, IMFMediaType *type)
195 av_freep(&avctx->extradata);
196 avctx->extradata = extradata;
197 avctx->extradata_size = sz;
203 static int mf_output_type_get(AVCodecContext *avctx)
205 MFContext *c = avctx->priv_data;
212 av_log(avctx, AV_LOG_ERROR, "could not get output type\n");
216 av_log(avctx, AV_LOG_VERBOSE, "final output type:\n");
217 ff_media_type_dump(avctx, type);
221 ret = mf_encv_output_type_get(avctx, type);
223 ret = mf_enca_output_type_get(avctx, type);
227 av_log(avctx, AV_LOG_ERROR, "output type not supported\n");
233 static int mf_sample_to_avpacket(AVCodecContext *avctx, IMFSample *sample, AVPacket *avpkt)
235 MFContext *c = avctx->priv_data;
248 if ((ret = ff_get_encode_buffer(avctx, avpkt, len, 0)) < 0)
266 avpkt->pts = avpkt->dts = mf_sample_get_pts(avctx, sample);
274 avpkt->dts = mf_from_mf_time(avctx, t);
288 static IMFSample *mf_a_avframe_to_sample(AVCodecContext *avctx, const AVFrame *frame)
290 MFContext *c = avctx->priv_data;
295 bps = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->ch_layout.nb_channels;
301 IMFSample_SetSampleDuration(sample, mf_to_mf_time(avctx, frame->nb_samples));
305 static IMFSample *mf_v_avframe_to_sample(AVCodecContext *avctx, const AVFrame *frame)
307 MFContext *c = avctx->priv_data;
315 size = av_image_get_buffer_size(avctx->pix_fmt, avctx->width, avctx->height, 1);
338 avctx->pix_fmt, avctx->width, avctx->height, 1);
347 IMFSample_SetSampleDuration(sample, mf_to_mf_time(avctx, frame->pkt_duration));
352 static IMFSample *mf_avframe_to_sample(AVCodecContext *avctx, const AVFrame *frame)
354 MFContext *c = avctx->priv_data;
358 sample = mf_a_avframe_to_sample(avctx, frame);
360 sample = mf_v_avframe_to_sample(avctx, frame);
364 mf_sample_set_pts(avctx, sample, frame->pts);
369 static int mf_send_sample(AVCodecContext *avctx, IMFSample *sample)
371 MFContext *c = avctx->priv_data;
377 if ((ret = mf_wait_events(avctx)) < 0)
389 av_log(avctx, AV_LOG_ERROR, "failed processing input: %s\n", ff_hr_str(hr));
396 av_log(avctx, AV_LOG_ERROR, "failed draining: %s\n", ff_hr_str(hr));
407 static int mf_receive_sample(AVCodecContext *avctx, IMFSample **out_sample)
409 MFContext *c = avctx->priv_data;
421 if ((ret = mf_wait_events(avctx)) < 0)
462 av_log(avctx, AV_LOG_WARNING, "stream format change\n");
463 ret = mf_choose_output_type(avctx);
467 ret = mf_setup_context(avctx);
474 av_log(avctx, AV_LOG_ERROR, "failed processing output: %s\n", ff_hr_str(hr));
489 static int mf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
491 MFContext *c = avctx->priv_data;
496 ret = ff_encode_get_frame(avctx, c->frame);
502 sample = mf_avframe_to_sample(avctx, c->frame);
513 ret = mf_send_sample(avctx, sample);
521 ret = mf_receive_sample(avctx, &sample);
525 ret = mf_sample_to_avpacket(avctx, sample, avpkt);
534 static int64_t mf_enca_output_score(AVCodecContext *avctx, IMFMediaType *type)
536 MFContext *c = avctx->priv_data;
543 if (!FAILED(hr) && t == avctx->sample_rate)
547 if (!FAILED(hr) && t == avctx->ch_layout.nb_channels)
559 int diff = (int)t - avctx->bit_rate / 8;
574 static int mf_enca_output_adjust(AVCodecContext *avctx, IMFMediaType *type)
578 //IMFAttributes_SetUINT32(type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, avctx->bit_rate / 8);
579 //IMFAttributes_SetUINT32(type, &MF_MT_AVG_BITRATE, avctx->bit_rate);
584 static int64_t mf_enca_input_score(AVCodecContext *avctx, IMFMediaType *type)
594 if (sformat == avctx->sample_fmt)
598 if (!FAILED(hr) && t == avctx->sample_rate)
602 if (!FAILED(hr) && t == avctx->ch_layout.nb_channels)
608 static int mf_enca_input_adjust(AVCodecContext *avctx, IMFMediaType *type)
614 if (sformat != avctx->sample_fmt) {
615 av_log(avctx, AV_LOG_ERROR, "unsupported input sample format set\n");
620 if (FAILED(hr) || t != avctx->sample_rate) {
621 av_log(avctx, AV_LOG_ERROR, "unsupported input sample rate set\n");
626 if (FAILED(hr) || t != avctx->ch_layout.nb_channels) {
627 av_log(avctx, AV_LOG_ERROR, "unsupported input channel number set\n");
634 static int64_t mf_encv_output_score(AVCodecContext *avctx, IMFMediaType *type)
636 MFContext *c = avctx->priv_data;
650 static int mf_encv_output_adjust(AVCodecContext *avctx, IMFMediaType *type)
652 MFContext *c = avctx->priv_data;
655 ff_MFSetAttributeSize((IMFAttributes *)type, &MF_MT_FRAME_SIZE, avctx->width, avctx->height);
658 if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
659 framerate = avctx->framerate;
661 framerate = av_inv_q(avctx->time_base);
662 framerate.den *= avctx->ticks_per_frame;
668 if (avctx->codec_id == AV_CODEC_ID_H264) {
670 switch (avctx->profile) {
681 IMFAttributes_SetUINT32(type, &MF_MT_AVG_BITRATE, avctx->bit_rate);
685 if (avctx->bit_rate)
686 ICodecAPI_SetValue(c->codec_api, &ff_CODECAPI_AVEncCommonMeanBitRate, FF_VAL_VT_UI4(avctx->bit_rate));
700 ICodecAPI_SetValue(c->codec_api, &ff_CODECAPI_AVEncMPVDefaultBPictureCount, FF_VAL_VT_UI4(avctx->max_b_frames));
701 avctx->has_b_frames = avctx->max_b_frames > 0;
712 static int64_t mf_encv_input_score(AVCodecContext *avctx, IMFMediaType *type)
715 if (pix_fmt != avctx->pix_fmt)
721 static int mf_encv_input_adjust(AVCodecContext *avctx, IMFMediaType *type)
724 if (pix_fmt != avctx->pix_fmt) {
725 av_log(avctx, AV_LOG_ERROR, "unsupported input pixel format set\n");
729 //ff_MFSetAttributeSize((IMFAttributes *)type, &MF_MT_FRAME_SIZE, avctx->width, avctx->height);
734 static int mf_choose_output_type(AVCodecContext *avctx)
736 MFContext *c = avctx->priv_data;
744 av_log(avctx, AV_LOG_VERBOSE, "output types:\n");
753 av_log(avctx, AV_LOG_VERBOSE, "(need to set input type)\n");
758 av_log(avctx, AV_LOG_ERROR, "error getting output type: %s\n", ff_hr_str(hr));
763 av_log(avctx, AV_LOG_VERBOSE, "output type %d:\n", n);
764 ff_media_type_dump(avctx, type);
767 score = mf_encv_output_score(avctx, type);
769 score = mf_enca_output_score(avctx, type);
785 av_log(avctx, AV_LOG_VERBOSE, "picking output type %d.\n", out_type_index);
796 ret = mf_encv_output_adjust(avctx, out_type);
798 ret = mf_enca_output_adjust(avctx, out_type);
802 av_log(avctx, AV_LOG_VERBOSE, "setting output type:\n");
803 ff_media_type_dump(avctx, out_type);
809 av_log(avctx, AV_LOG_VERBOSE, "rejected - need to set input type\n");
812 av_log(avctx, AV_LOG_ERROR, "could not set output type (%s)\n", ff_hr_str(hr));
823 static int mf_choose_input_type(AVCodecContext *avctx)
825 MFContext *c = avctx->priv_data;
833 av_log(avctx, AV_LOG_VERBOSE, "input types:\n");
842 av_log(avctx, AV_LOG_VERBOSE, "(need to set output type 1)\n");
847 av_log(avctx, AV_LOG_ERROR, "error getting input type: %s\n", ff_hr_str(hr));
852 av_log(avctx, AV_LOG_VERBOSE, "input type %d:\n", n);
853 ff_media_type_dump(avctx, type);
856 score = mf_encv_input_score(avctx, type);
858 score = mf_enca_input_score(avctx, type);
874 av_log(avctx, AV_LOG_VERBOSE, "picking input type %d.\n", in_type_index);
877 av_log(avctx, AV_LOG_VERBOSE, "(need to set output type 2)\n");
884 ret = mf_encv_input_adjust(avctx, in_type);
886 ret = mf_enca_input_adjust(avctx, in_type);
890 av_log(avctx, AV_LOG_VERBOSE, "setting input type:\n");
891 ff_media_type_dump(avctx, in_type);
897 av_log(avctx, AV_LOG_VERBOSE, "rejected - need to set output type\n");
900 av_log(avctx, AV_LOG_ERROR, "could not set input type (%s)\n", ff_hr_str(hr));
911 static int mf_negotiate_types(AVCodecContext *avctx)
921 ret = mf_choose_input_type(avctx);
925 ret = mf_choose_output_type(avctx);
931 av_log(avctx, AV_LOG_ERROR, "format negotiation failed (%d/%d)\n",
938 static int mf_setup_context(AVCodecContext *avctx)
940 MFContext *c = avctx->priv_data;
947 av_log(avctx, AV_LOG_VERBOSE, "in_info: size=%d, align=%d\n",
956 av_log(avctx, AV_LOG_VERBOSE, "out_info: size=%d, align=%d%s\n",
960 if ((ret = mf_output_type_get(avctx)) < 0)
966 static int mf_unlock_async(AVCodecContext *avctx)
968 MFContext *c = avctx->priv_data;
981 av_log(avctx, AV_LOG_ERROR, "error retrieving MFT attributes: %s\n", ff_hr_str(hr));
987 av_log(avctx, AV_LOG_ERROR, "error querying async: %s\n", ff_hr_str(hr));
992 av_log(avctx, AV_LOG_ERROR, "hardware MFT is not async\n");
998 av_log(avctx, AV_LOG_ERROR, "could not set async unlock: %s\n", ff_hr_str(hr));
1004 av_log(avctx, AV_LOG_ERROR, "could not get async interface\n");
1045 static int mf_init_encoder(AVCodecContext *avctx)
1047 MFContext *c = avctx->priv_data;
1050 const CLSID *subtype = ff_codec_to_mf_subtype(avctx->codec_id);
1057 c->is_audio = avctx->codec_type == AVMEDIA_TYPE_AUDIO;
1069 if ((ret = mf_create(avctx, &c->functions, &c->mft, avctx->codec, use_hw)) < 0)
1072 if ((ret = mf_unlock_async(avctx)) < 0)
1077 av_log(avctx, AV_LOG_VERBOSE, "MFT supports ICodecAPI.\n");
1084 av_log(avctx, AV_LOG_ERROR, "could not get stream IDs (%s)\n", ff_hr_str(hr));
1088 if ((ret = mf_negotiate_types(avctx)) < 0)
1091 if ((ret = mf_setup_context(avctx)) < 0)
1096 av_log(avctx, AV_LOG_ERROR, "could not start streaming (%s)\n", ff_hr_str(hr));
1102 av_log(avctx, AV_LOG_ERROR, "could not start stream (%s)\n", ff_hr_str(hr));
1106 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER && c->async_events &&
1107 c->is_video && !avctx->extradata) {
1109 av_log(avctx, AV_LOG_VERBOSE, "Awaiting extradata\n");
1121 mf_output_type_get(avctx);
1122 if (avctx->extradata)
1126 av_log(avctx, AV_LOG_VERBOSE, "%s extradata in %d ms\n",
1127 avctx->extradata ? "Got" : "Didn't get", total / 1000);
1157 static int mf_load_library(AVCodecContext *avctx)
1159 MFContext *c = avctx->priv_data;
1181 static int mf_close(AVCodecContext *avctx)
1183 MFContext *c = avctx->priv_data;
1203 av_freep(&avctx->extradata);
1204 avctx->extradata_size = 0;
1209 static int mf_init(AVCodecContext *avctx)
1212 if ((ret = mf_load_library(avctx)) == 0) {
1213 if ((ret = mf_init_encoder(avctx)) == 0) {
1217 mf_close(avctx);