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.

156 lines
5.5KB

  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. AVFilterBufferRef *avfilter_get_video_buffer_ref_from_frame(const AVFrame *frame,
  29. int perms)
  30. {
  31. AVFilterBufferRef *picref =
  32. avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize, perms,
  33. frame->width, frame->height,
  34. frame->format);
  35. if (!picref)
  36. return NULL;
  37. if (avfilter_copy_frame_props(picref, frame) < 0) {
  38. picref->buf->data[0] = NULL;
  39. avfilter_unref_bufferp(&picref);
  40. }
  41. return picref;
  42. }
  43. AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_frame(const AVFrame *frame,
  44. int perms)
  45. {
  46. AVFilterBufferRef *samplesref;
  47. int channels = av_frame_get_channels(frame);
  48. int64_t layout = av_frame_get_channel_layout(frame);
  49. if (layout && av_get_channel_layout_nb_channels(layout) != av_frame_get_channels(frame)) {
  50. av_log(0, AV_LOG_ERROR, "Layout indicates a different number of channels than actually present\n");
  51. return NULL;
  52. }
  53. samplesref = avfilter_get_audio_buffer_ref_from_arrays_channels(
  54. (uint8_t **)frame->extended_data, frame->linesize[0], perms,
  55. frame->nb_samples, frame->format, channels, layout);
  56. if (!samplesref)
  57. return NULL;
  58. if (avfilter_copy_frame_props(samplesref, frame) < 0) {
  59. samplesref->buf->data[0] = NULL;
  60. avfilter_unref_bufferp(&samplesref);
  61. }
  62. return samplesref;
  63. }
  64. AVFilterBufferRef *avfilter_get_buffer_ref_from_frame(enum AVMediaType type,
  65. const AVFrame *frame,
  66. int perms)
  67. {
  68. switch (type) {
  69. case AVMEDIA_TYPE_VIDEO:
  70. return avfilter_get_video_buffer_ref_from_frame(frame, perms);
  71. case AVMEDIA_TYPE_AUDIO:
  72. return avfilter_get_audio_buffer_ref_from_frame(frame, perms);
  73. default:
  74. return NULL;
  75. }
  76. }
  77. int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
  78. {
  79. int planes, nb_channels;
  80. if (!dst)
  81. return AVERROR(EINVAL);
  82. /* abort in case the src is NULL and dst is not, avoid inconsistent state in dst */
  83. av_assert0(src);
  84. memcpy(dst->data, src->data, sizeof(dst->data));
  85. memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
  86. dst->pts = src->pts;
  87. dst->format = src->format;
  88. av_frame_set_pkt_pos(dst, src->pos);
  89. switch (src->type) {
  90. case AVMEDIA_TYPE_VIDEO:
  91. av_assert0(src->video);
  92. dst->width = src->video->w;
  93. dst->height = src->video->h;
  94. dst->sample_aspect_ratio = src->video->sample_aspect_ratio;
  95. dst->interlaced_frame = src->video->interlaced;
  96. dst->top_field_first = src->video->top_field_first;
  97. dst->key_frame = src->video->key_frame;
  98. dst->pict_type = src->video->pict_type;
  99. break;
  100. case AVMEDIA_TYPE_AUDIO:
  101. av_assert0(src->audio);
  102. nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
  103. planes = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
  104. if (planes > FF_ARRAY_ELEMS(dst->data)) {
  105. dst->extended_data = av_mallocz(planes * sizeof(*dst->extended_data));
  106. if (!dst->extended_data)
  107. return AVERROR(ENOMEM);
  108. memcpy(dst->extended_data, src->extended_data,
  109. planes * sizeof(*dst->extended_data));
  110. } else
  111. dst->extended_data = dst->data;
  112. dst->nb_samples = src->audio->nb_samples;
  113. av_frame_set_sample_rate (dst, src->audio->sample_rate);
  114. av_frame_set_channel_layout(dst, src->audio->channel_layout);
  115. av_frame_set_channels (dst, src->audio->channels);
  116. break;
  117. default:
  118. return AVERROR(EINVAL);
  119. }
  120. return 0;
  121. }
  122. #if FF_API_FILL_FRAME
  123. int avfilter_fill_frame_from_audio_buffer_ref(AVFrame *frame,
  124. const AVFilterBufferRef *samplesref)
  125. {
  126. return avfilter_copy_buf_props(frame, samplesref);
  127. }
  128. int avfilter_fill_frame_from_video_buffer_ref(AVFrame *frame,
  129. const AVFilterBufferRef *picref)
  130. {
  131. return avfilter_copy_buf_props(frame, picref);
  132. }
  133. int avfilter_fill_frame_from_buffer_ref(AVFrame *frame,
  134. const AVFilterBufferRef *ref)
  135. {
  136. return avfilter_copy_buf_props(frame, ref);
  137. }
  138. #endif