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.

122 lines
3.4KB

  1. /*
  2. * AVFrame wrapper
  3. * Copyright (c) 2015 Luca Barbato
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Simple wrapper to store an AVFrame and forward it as AVPacket.
  24. */
  25. #include "avcodec.h"
  26. #include "internal.h"
  27. #include "libavutil/internal.h"
  28. #include "libavutil/frame.h"
  29. #include "libavutil/buffer.h"
  30. #include "libavutil/pixdesc.h"
  31. static void wrapped_avframe_release_buffer(void *unused, uint8_t *data)
  32. {
  33. AVFrame *frame = (AVFrame *)data;
  34. av_frame_free(&frame);
  35. }
  36. static int wrapped_avframe_encode(AVCodecContext *avctx, AVPacket *pkt,
  37. const AVFrame *frame, int *got_packet)
  38. {
  39. AVFrame *wrapped = av_frame_clone(frame);
  40. uint8_t *data;
  41. int size = sizeof(*wrapped) + AV_INPUT_BUFFER_PADDING_SIZE;
  42. if (!wrapped)
  43. return AVERROR(ENOMEM);
  44. data = av_mallocz(size);
  45. if (!data) {
  46. av_frame_free(&wrapped);
  47. return AVERROR(ENOMEM);
  48. }
  49. pkt->buf = av_buffer_create(data, size,
  50. wrapped_avframe_release_buffer, NULL,
  51. AV_BUFFER_FLAG_READONLY);
  52. if (!pkt->buf) {
  53. av_frame_free(&wrapped);
  54. av_freep(&data);
  55. return AVERROR(ENOMEM);
  56. }
  57. av_frame_move_ref((AVFrame*)data, wrapped);
  58. av_frame_free(&wrapped);
  59. pkt->data = data;
  60. pkt->size = sizeof(*wrapped);
  61. pkt->flags |= AV_PKT_FLAG_KEY;
  62. *got_packet = 1;
  63. return 0;
  64. }
  65. static int wrapped_avframe_decode(AVCodecContext *avctx, void *data,
  66. int *got_frame, AVPacket *pkt)
  67. {
  68. AVFrame *in, *out;
  69. int err;
  70. if (!(pkt->flags & AV_PKT_FLAG_TRUSTED)) {
  71. // This decoder is not usable with untrusted input.
  72. return AVERROR(EPERM);
  73. }
  74. if (pkt->size < sizeof(AVFrame))
  75. return AVERROR(EINVAL);
  76. in = (AVFrame*)pkt->data;
  77. out = data;
  78. err = ff_decode_frame_props(avctx, out);
  79. if (err < 0)
  80. return err;
  81. av_frame_move_ref(out, in);
  82. *got_frame = 1;
  83. return 0;
  84. }
  85. AVCodec ff_wrapped_avframe_encoder = {
  86. .name = "wrapped_avframe",
  87. .long_name = NULL_IF_CONFIG_SMALL("AVFrame to AVPacket passthrough"),
  88. .type = AVMEDIA_TYPE_VIDEO,
  89. .id = AV_CODEC_ID_WRAPPED_AVFRAME,
  90. .encode2 = wrapped_avframe_encode,
  91. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  92. };
  93. AVCodec ff_wrapped_avframe_decoder = {
  94. .name = "wrapped_avframe",
  95. .long_name = NULL_IF_CONFIG_SMALL("AVPacket to AVFrame passthrough"),
  96. .type = AVMEDIA_TYPE_VIDEO,
  97. .id = AV_CODEC_ID_WRAPPED_AVFRAME,
  98. .decode = wrapped_avframe_decode,
  99. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  100. };