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.

140 lines
5.0KB

  1. /*
  2. * Copyright 2011 Stefano Sabatini | stefasab at gmail.com
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * libavcodec/libavfilter gluing utilities
  23. */
  24. #include "avcodec.h"
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/opt.h"
  28. #if FF_API_AVFILTERBUFFER
  29. FF_DISABLE_DEPRECATION_WARNINGS
  30. AVFilterBufferRef *avfilter_get_video_buffer_ref_from_frame(const AVFrame *frame,
  31. int perms)
  32. {
  33. AVFilterBufferRef *picref =
  34. avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize, perms,
  35. frame->width, frame->height,
  36. frame->format);
  37. if (!picref)
  38. return NULL;
  39. if (avfilter_copy_frame_props(picref, frame) < 0) {
  40. picref->buf->data[0] = NULL;
  41. avfilter_unref_bufferp(&picref);
  42. }
  43. return picref;
  44. }
  45. AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_frame(const AVFrame *frame,
  46. int perms)
  47. {
  48. AVFilterBufferRef *samplesref;
  49. int channels = av_frame_get_channels(frame);
  50. int64_t layout = av_frame_get_channel_layout(frame);
  51. if (layout && av_get_channel_layout_nb_channels(layout) != av_frame_get_channels(frame)) {
  52. av_log(NULL, AV_LOG_ERROR, "Layout indicates a different number of channels than actually present\n");
  53. return NULL;
  54. }
  55. samplesref = avfilter_get_audio_buffer_ref_from_arrays_channels(
  56. (uint8_t **)frame->extended_data, frame->linesize[0], perms,
  57. frame->nb_samples, frame->format, channels, layout);
  58. if (!samplesref)
  59. return NULL;
  60. if (avfilter_copy_frame_props(samplesref, frame) < 0) {
  61. samplesref->buf->data[0] = NULL;
  62. avfilter_unref_bufferp(&samplesref);
  63. }
  64. return samplesref;
  65. }
  66. AVFilterBufferRef *avfilter_get_buffer_ref_from_frame(enum AVMediaType type,
  67. const AVFrame *frame,
  68. int perms)
  69. {
  70. switch (type) {
  71. case AVMEDIA_TYPE_VIDEO:
  72. return avfilter_get_video_buffer_ref_from_frame(frame, perms);
  73. case AVMEDIA_TYPE_AUDIO:
  74. return avfilter_get_audio_buffer_ref_from_frame(frame, perms);
  75. default:
  76. return NULL;
  77. }
  78. }
  79. int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
  80. {
  81. int planes, nb_channels;
  82. if (!dst)
  83. return AVERROR(EINVAL);
  84. /* abort in case the src is NULL and dst is not, avoid inconsistent state in dst */
  85. av_assert0(src);
  86. memcpy(dst->data, src->data, sizeof(dst->data));
  87. memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
  88. dst->pts = src->pts;
  89. dst->format = src->format;
  90. av_frame_set_pkt_pos(dst, src->pos);
  91. switch (src->type) {
  92. case AVMEDIA_TYPE_VIDEO:
  93. av_assert0(src->video);
  94. dst->width = src->video->w;
  95. dst->height = src->video->h;
  96. dst->sample_aspect_ratio = src->video->sample_aspect_ratio;
  97. dst->interlaced_frame = src->video->interlaced;
  98. dst->top_field_first = src->video->top_field_first;
  99. dst->key_frame = src->video->key_frame;
  100. dst->pict_type = src->video->pict_type;
  101. break;
  102. case AVMEDIA_TYPE_AUDIO:
  103. av_assert0(src->audio);
  104. nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
  105. planes = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
  106. if (planes > FF_ARRAY_ELEMS(dst->data)) {
  107. dst->extended_data = av_mallocz_array(planes, sizeof(*dst->extended_data));
  108. if (!dst->extended_data)
  109. return AVERROR(ENOMEM);
  110. memcpy(dst->extended_data, src->extended_data,
  111. planes * sizeof(*dst->extended_data));
  112. } else
  113. dst->extended_data = dst->data;
  114. dst->nb_samples = src->audio->nb_samples;
  115. av_frame_set_sample_rate (dst, src->audio->sample_rate);
  116. av_frame_set_channel_layout(dst, src->audio->channel_layout);
  117. av_frame_set_channels (dst, src->audio->channels);
  118. break;
  119. default:
  120. return AVERROR(EINVAL);
  121. }
  122. return 0;
  123. }
  124. FF_ENABLE_DEPRECATION_WARNINGS
  125. #endif