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.

264 lines
7.5KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. AVBufferRef *hwframes_ref;
  32. AVHWFramesContext *hwframes;
  33. char *device_type;
  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 (ctx->hwdevice_ref) {
  43. /* We already have a specified device. */
  44. } else if (avctx->hw_device_ctx) {
  45. if (ctx->device_type) {
  46. err = av_hwdevice_ctx_create_derived(
  47. &ctx->hwdevice_ref,
  48. av_hwdevice_find_type_by_name(ctx->device_type),
  49. avctx->hw_device_ctx, 0);
  50. if (err < 0)
  51. return err;
  52. } else {
  53. ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx);
  54. if (!ctx->hwdevice_ref)
  55. return AVERROR(ENOMEM);
  56. }
  57. } else {
  58. av_log(ctx, AV_LOG_ERROR, "A hardware device reference is required "
  59. "to upload frames to.\n");
  60. return AVERROR(EINVAL);
  61. }
  62. constraints = av_hwdevice_get_hwframe_constraints(ctx->hwdevice_ref, NULL);
  63. if (!constraints) {
  64. err = AVERROR(EINVAL);
  65. goto fail;
  66. }
  67. input_pix_fmts = constraints->valid_sw_formats;
  68. output_pix_fmts = constraints->valid_hw_formats;
  69. input_formats = ff_make_format_list(output_pix_fmts);
  70. if (!input_formats) {
  71. err = AVERROR(ENOMEM);
  72. goto fail;
  73. }
  74. if (input_pix_fmts) {
  75. for (i = 0; input_pix_fmts[i] != AV_PIX_FMT_NONE; i++) {
  76. err = ff_add_format(&input_formats, input_pix_fmts[i]);
  77. if (err < 0)
  78. goto fail;
  79. }
  80. }
  81. if ((err = ff_formats_ref(input_formats, &avctx->inputs[0]->out_formats)) < 0 ||
  82. (err = ff_formats_ref(ff_make_format_list(output_pix_fmts),
  83. &avctx->outputs[0]->in_formats)) < 0)
  84. goto fail;
  85. av_hwframe_constraints_free(&constraints);
  86. return 0;
  87. fail:
  88. av_buffer_unref(&ctx->hwdevice_ref);
  89. av_hwframe_constraints_free(&constraints);
  90. return err;
  91. }
  92. static int hwupload_config_output(AVFilterLink *outlink)
  93. {
  94. AVFilterContext *avctx = outlink->src;
  95. AVFilterLink *inlink = avctx->inputs[0];
  96. HWUploadContext *ctx = avctx->priv;
  97. int err;
  98. av_buffer_unref(&ctx->hwframes_ref);
  99. if (inlink->format == outlink->format) {
  100. // The input is already a hardware format, so we just want to
  101. // pass through the input frames in their own hardware context.
  102. if (!inlink->hw_frames_ctx) {
  103. av_log(ctx, AV_LOG_ERROR, "No input hwframe context.\n");
  104. return AVERROR(EINVAL);
  105. }
  106. outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
  107. if (!outlink->hw_frames_ctx)
  108. return AVERROR(ENOMEM);
  109. return 0;
  110. }
  111. ctx->hwframes_ref = av_hwframe_ctx_alloc(ctx->hwdevice_ref);
  112. if (!ctx->hwframes_ref)
  113. return AVERROR(ENOMEM);
  114. ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
  115. av_log(ctx, AV_LOG_DEBUG, "Surface format is %s.\n",
  116. av_get_pix_fmt_name(inlink->format));
  117. ctx->hwframes->format = outlink->format;
  118. if (inlink->hw_frames_ctx) {
  119. AVHWFramesContext *in_hwframe_ctx =
  120. (AVHWFramesContext*)inlink->hw_frames_ctx->data;
  121. ctx->hwframes->sw_format = in_hwframe_ctx->sw_format;
  122. } else {
  123. ctx->hwframes->sw_format = inlink->format;
  124. }
  125. ctx->hwframes->width = inlink->w;
  126. ctx->hwframes->height = inlink->h;
  127. if (avctx->extra_hw_frames >= 0)
  128. ctx->hwframes->initial_pool_size = 2 + avctx->extra_hw_frames;
  129. err = av_hwframe_ctx_init(ctx->hwframes_ref);
  130. if (err < 0)
  131. goto fail;
  132. outlink->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
  133. if (!outlink->hw_frames_ctx) {
  134. err = AVERROR(ENOMEM);
  135. goto fail;
  136. }
  137. return 0;
  138. fail:
  139. av_buffer_unref(&ctx->hwframes_ref);
  140. return err;
  141. }
  142. static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
  143. {
  144. AVFilterContext *avctx = link->dst;
  145. AVFilterLink *outlink = avctx->outputs[0];
  146. HWUploadContext *ctx = avctx->priv;
  147. AVFrame *output = NULL;
  148. int err;
  149. if (input->format == outlink->format)
  150. return ff_filter_frame(outlink, input);
  151. output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  152. if (!output) {
  153. av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame to upload to.\n");
  154. err = AVERROR(ENOMEM);
  155. goto fail;
  156. }
  157. output->width = input->width;
  158. output->height = input->height;
  159. err = av_hwframe_transfer_data(output, input, 0);
  160. if (err < 0) {
  161. av_log(ctx, AV_LOG_ERROR, "Failed to upload frame: %d.\n", err);
  162. goto fail;
  163. }
  164. err = av_frame_copy_props(output, input);
  165. if (err < 0)
  166. goto fail;
  167. av_frame_free(&input);
  168. return ff_filter_frame(outlink, output);
  169. fail:
  170. av_frame_free(&input);
  171. av_frame_free(&output);
  172. return err;
  173. }
  174. static av_cold void hwupload_uninit(AVFilterContext *avctx)
  175. {
  176. HWUploadContext *ctx = avctx->priv;
  177. av_buffer_unref(&ctx->hwframes_ref);
  178. av_buffer_unref(&ctx->hwdevice_ref);
  179. }
  180. #define OFFSET(x) offsetof(HWUploadContext, x)
  181. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
  182. static const AVOption hwupload_options[] = {
  183. {
  184. "derive_device", "Derive a new device of this type",
  185. OFFSET(device_type), AV_OPT_TYPE_STRING,
  186. { .str = NULL }, 0, 0, FLAGS
  187. },
  188. {
  189. NULL
  190. }
  191. };
  192. AVFILTER_DEFINE_CLASS(hwupload);
  193. static const AVFilterPad hwupload_inputs[] = {
  194. {
  195. .name = "default",
  196. .type = AVMEDIA_TYPE_VIDEO,
  197. .filter_frame = hwupload_filter_frame,
  198. },
  199. { NULL }
  200. };
  201. static const AVFilterPad hwupload_outputs[] = {
  202. {
  203. .name = "default",
  204. .type = AVMEDIA_TYPE_VIDEO,
  205. .config_props = hwupload_config_output,
  206. },
  207. { NULL }
  208. };
  209. AVFilter ff_vf_hwupload = {
  210. .name = "hwupload",
  211. .description = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware frame"),
  212. .uninit = hwupload_uninit,
  213. .query_formats = hwupload_query_formats,
  214. .priv_size = sizeof(HWUploadContext),
  215. .priv_class = &hwupload_class,
  216. .inputs = hwupload_inputs,
  217. .outputs = hwupload_outputs,
  218. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  219. };