+AVHWDeviceType FFVideoStream::decode_hw_activate()
+{
+ AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
+ const char *hw_dev = ffmpeg->opt_hw_dev;
+ if( !hw_dev ) hw_dev = getenv("CIN_HW_DEV");
+ if( !hw_dev ) hw_dev = ffmpeg->ff_hw_dev();
+ if( hw_dev && *hw_dev && strcmp(_("none"), hw_dev) ) {
+ type = av_hwdevice_find_type_by_name(hw_dev);
+ if( type == AV_HWDEVICE_TYPE_NONE ) {
+ fprintf(stderr, "Device type %s is not supported.\n", hw_dev);
+ fprintf(stderr, "Available device types:");
+ while( (type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE )
+ fprintf(stderr, " %s", av_hwdevice_get_type_name(type));
+ fprintf(stderr, "\n");
+ }
+ }
+ return type;
+}
+
+int FFVideoStream::decode_hw_format(AVCodec *decoder, AVHWDeviceType type)
+{
+ int ret = 0;
+ hw_pix_fmt = AV_PIX_FMT_NONE;
+ for( int i=0; ; ++i ) {
+ const AVCodecHWConfig *config = avcodec_get_hw_config(decoder, i);
+ if( !config ) {
+ fprintf(stderr, "Decoder %s does not support device type %s.\n",
+ decoder->name, av_hwdevice_get_type_name(type));
+ break;
+ }
+ if( (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) != 0 &&
+ config->device_type == type ) {
+ hw_pix_fmt = config->pix_fmt;
+ break;
+ }
+ }
+ if( hw_pix_fmt >= 0 ) {
+ hw_pixfmt = hw_pix_fmt;
+ avctx->get_format = get_hw_format;
+ ret = av_hwdevice_ctx_create(&hw_device_ctx, type, 0, 0, 0);
+ if( ret >= 0 ) {
+ avctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
+ ret = 1;
+ }
+ else
+ ff_err(ret, "Failed HW device create.\ndev:%s\n",
+ av_hwdevice_get_type_name(type));
+ }
+ return ret;
+}
+
+AVHWDeviceType FFVideoStream::encode_hw_activate(const char *hw_dev)
+{
+ AVBufferRef *hw_device_ctx = 0;
+ AVBufferRef *hw_frames_ref = 0;
+ AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
+ if( strcmp(_("none"), hw_dev) ) {
+ type = av_hwdevice_find_type_by_name(hw_dev);
+ if( type != AV_HWDEVICE_TYPE_VAAPI ) {
+ fprintf(stderr, "currently, only vaapi hw encode is supported\n");
+ type = AV_HWDEVICE_TYPE_NONE;
+ }
+ }
+ if( type != AV_HWDEVICE_TYPE_NONE ) {
+ int ret = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, 0, 0, 0);
+ if( ret < 0 ) {
+ ff_err(ret, "Failed to create a HW device.\n");
+ type = AV_HWDEVICE_TYPE_NONE;
+ }
+ }
+ if( type != AV_HWDEVICE_TYPE_NONE ) {
+ hw_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx);
+ if( !hw_frames_ref ) {
+ fprintf(stderr, "Failed to create HW frame context.\n");
+ type = AV_HWDEVICE_TYPE_NONE;
+ }
+ }
+ if( type != AV_HWDEVICE_TYPE_NONE ) {
+ AVHWFramesContext *frames_ctx = (AVHWFramesContext *)(hw_frames_ref->data);
+ frames_ctx->format = AV_PIX_FMT_VAAPI;
+ frames_ctx->sw_format = AV_PIX_FMT_NV12;
+ frames_ctx->width = width;
+ frames_ctx->height = height;
+ frames_ctx->initial_pool_size = 0; // 200;
+ int ret = av_hwframe_ctx_init(hw_frames_ref);
+ if( ret >= 0 ) {
+ avctx->hw_frames_ctx = av_buffer_ref(hw_frames_ref);
+ if( !avctx->hw_frames_ctx ) ret = AVERROR(ENOMEM);
+ }
+ if( ret < 0 ) {
+ ff_err(ret, "Failed to initialize HW frame context.\n");
+ type = AV_HWDEVICE_TYPE_NONE;
+ }
+ av_buffer_unref(&hw_frames_ref);
+ }
+ return type;
+}
+
+int FFVideoStream::encode_hw_write(FFrame *picture)
+{
+ int ret = 0;
+ AVFrame *hw_frm = 0;
+ switch( avctx->pix_fmt ) {
+ case AV_PIX_FMT_VAAPI:
+ hw_frm = av_frame_alloc();
+ if( !hw_frm ) { ret = AVERROR(ENOMEM); break; }
+ ret = av_hwframe_get_buffer(avctx->hw_frames_ctx, hw_frm, 0);
+ if( ret < 0 ) break;
+ ret = av_hwframe_transfer_data(hw_frm, *picture, 0);
+ if( ret < 0 ) break;
+ picture->set_hw_frame(hw_frm);
+ return 0;
+ default:
+ return 0;
+ }
+ av_frame_free(&hw_frm);
+ ff_err(ret, "Error while transferring frame data to GPU.\n");
+ return ret;
+}
+