You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
6.7KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/buffer.h"
  19. #include "libavutil/hwcontext.h"
  20. #include "libavutil/hwcontext_internal.h"
  21. #include "libavutil/log.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "libavutil/opt.h"
  24. #include "avfilter.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. typedef struct HWUploadContext {
  29. const AVClass *class;
  30. AVBufferRef *hwdevice_ref;
  31. AVHWDeviceContext *hwdevice;
  32. AVBufferRef *hwframes_ref;
  33. AVHWFramesContext *hwframes;
  34. } HWUploadContext;
  35. static int hwupload_query_formats(AVFilterContext *avctx)
  36. {
  37. HWUploadContext *ctx = avctx->priv;
  38. AVHWFramesConstraints *constraints = NULL;
  39. const enum AVPixelFormat *input_pix_fmts, *output_pix_fmts;
  40. AVFilterFormats *input_formats = NULL;
  41. int err, i;
  42. if (!avctx->hw_device_ctx) {
  43. av_log(ctx, AV_LOG_ERROR, "A hardware device reference is required "
  44. "to upload frames to.\n");
  45. return AVERROR(EINVAL);
  46. }
  47. ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx);
  48. if (!ctx->hwdevice_ref)
  49. return AVERROR(ENOMEM);
  50. ctx->hwdevice = (AVHWDeviceContext*)ctx->hwdevice_ref->data;
  51. constraints = av_hwdevice_get_hwframe_constraints(ctx->hwdevice_ref, NULL);
  52. if (!constraints) {
  53. err = AVERROR(EINVAL);
  54. goto fail;
  55. }
  56. input_pix_fmts = constraints->valid_sw_formats;
  57. output_pix_fmts = constraints->valid_hw_formats;
  58. input_formats = ff_make_format_list(output_pix_fmts);
  59. if (!input_formats) {
  60. err = AVERROR(ENOMEM);
  61. goto fail;
  62. }
  63. if (input_pix_fmts) {
  64. for (i = 0; input_pix_fmts[i] != AV_PIX_FMT_NONE; i++) {
  65. err = ff_add_format(&input_formats, input_pix_fmts[i]);
  66. if (err < 0) {
  67. ff_formats_unref(&input_formats);
  68. goto fail;
  69. }
  70. }
  71. }
  72. ff_formats_ref(input_formats, &avctx->inputs[0]->out_formats);
  73. ff_formats_ref(ff_make_format_list(output_pix_fmts),
  74. &avctx->outputs[0]->in_formats);
  75. av_hwframe_constraints_free(&constraints);
  76. return 0;
  77. fail:
  78. av_buffer_unref(&ctx->hwdevice_ref);
  79. av_hwframe_constraints_free(&constraints);
  80. return err;
  81. }
  82. static int hwupload_config_output(AVFilterLink *outlink)
  83. {
  84. AVFilterContext *avctx = outlink->src;
  85. AVFilterLink *inlink = avctx->inputs[0];
  86. HWUploadContext *ctx = avctx->priv;
  87. int err;
  88. av_buffer_unref(&ctx->hwframes_ref);
  89. if (inlink->format == outlink->format) {
  90. // The input is already a hardware format, so we just want to
  91. // pass through the input frames in their own hardware context.
  92. if (!inlink->hw_frames_ctx) {
  93. av_log(ctx, AV_LOG_ERROR, "No input hwframe context.\n");
  94. return AVERROR(EINVAL);
  95. }
  96. outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
  97. if (!outlink->hw_frames_ctx)
  98. return AVERROR(ENOMEM);
  99. return 0;
  100. }
  101. ctx->hwframes_ref = av_hwframe_ctx_alloc(ctx->hwdevice_ref);
  102. if (!ctx->hwframes_ref)
  103. return AVERROR(ENOMEM);
  104. ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
  105. av_log(ctx, AV_LOG_DEBUG, "Surface format is %s.\n",
  106. av_get_pix_fmt_name(inlink->format));
  107. ctx->hwframes->format = outlink->format;
  108. ctx->hwframes->sw_format = inlink->format;
  109. ctx->hwframes->width = inlink->w;
  110. ctx->hwframes->height = inlink->h;
  111. if (avctx->extra_hw_frames >= 0)
  112. ctx->hwframes->initial_pool_size = 2 + avctx->extra_hw_frames;
  113. err = av_hwframe_ctx_init(ctx->hwframes_ref);
  114. if (err < 0)
  115. goto fail;
  116. outlink->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
  117. if (!outlink->hw_frames_ctx) {
  118. err = AVERROR(ENOMEM);
  119. goto fail;
  120. }
  121. return 0;
  122. fail:
  123. av_buffer_unref(&ctx->hwframes_ref);
  124. return err;
  125. }
  126. static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
  127. {
  128. AVFilterContext *avctx = link->dst;
  129. AVFilterLink *outlink = avctx->outputs[0];
  130. HWUploadContext *ctx = avctx->priv;
  131. AVFrame *output = NULL;
  132. int err;
  133. if (input->format == outlink->format)
  134. return ff_filter_frame(outlink, input);
  135. output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  136. if (!output) {
  137. av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame to upload to.\n");
  138. err = AVERROR(ENOMEM);
  139. goto fail;
  140. }
  141. output->width = input->width;
  142. output->height = input->height;
  143. err = av_hwframe_transfer_data(output, input, 0);
  144. if (err < 0) {
  145. av_log(ctx, AV_LOG_ERROR, "Failed to upload frame: %d.\n", err);
  146. goto fail;
  147. }
  148. err = av_frame_copy_props(output, input);
  149. if (err < 0)
  150. goto fail;
  151. av_frame_free(&input);
  152. return ff_filter_frame(outlink, output);
  153. fail:
  154. av_frame_free(&input);
  155. av_frame_free(&output);
  156. return err;
  157. }
  158. static av_cold void hwupload_uninit(AVFilterContext *avctx)
  159. {
  160. HWUploadContext *ctx = avctx->priv;
  161. av_buffer_unref(&ctx->hwframes_ref);
  162. av_buffer_unref(&ctx->hwdevice_ref);
  163. }
  164. static const AVClass hwupload_class = {
  165. .class_name = "hwupload",
  166. .item_name = av_default_item_name,
  167. .option = NULL,
  168. .version = LIBAVUTIL_VERSION_INT,
  169. };
  170. static const AVFilterPad hwupload_inputs[] = {
  171. {
  172. .name = "default",
  173. .type = AVMEDIA_TYPE_VIDEO,
  174. .filter_frame = hwupload_filter_frame,
  175. },
  176. { NULL }
  177. };
  178. static const AVFilterPad hwupload_outputs[] = {
  179. {
  180. .name = "default",
  181. .type = AVMEDIA_TYPE_VIDEO,
  182. .config_props = hwupload_config_output,
  183. },
  184. { NULL }
  185. };
  186. AVFilter ff_vf_hwupload = {
  187. .name = "hwupload",
  188. .description = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware frame"),
  189. .uninit = hwupload_uninit,
  190. .query_formats = hwupload_query_formats,
  191. .priv_size = sizeof(HWUploadContext),
  192. .priv_class = &hwupload_class,
  193. .inputs = hwupload_inputs,
  194. .outputs = hwupload_outputs,
  195. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  196. };