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.

169 lines
6.1KB

  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. /**
  19. * @file
  20. * libavcodec/libavfilter gluing utilities
  21. */
  22. #include "avcodec.h"
  23. #include "libavutil/opt.h"
  24. int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
  25. {
  26. dst->pts = src->pts;
  27. dst->pos = src->pkt_pos;
  28. dst->format = src->format;
  29. switch (dst->type) {
  30. case AVMEDIA_TYPE_VIDEO:
  31. dst->video->w = src->width;
  32. dst->video->h = src->height;
  33. dst->video->sample_aspect_ratio = src->sample_aspect_ratio;
  34. dst->video->interlaced = src->interlaced_frame;
  35. dst->video->top_field_first = src->top_field_first;
  36. dst->video->key_frame = src->key_frame;
  37. dst->video->pict_type = src->pict_type;
  38. break;
  39. case AVMEDIA_TYPE_AUDIO:
  40. dst->audio->sample_rate = src->sample_rate;
  41. dst->audio->channel_layout = src->channel_layout;
  42. break;
  43. default:
  44. return AVERROR(EINVAL);
  45. }
  46. return 0;
  47. }
  48. int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
  49. {
  50. int planes, nb_channels;
  51. memcpy(dst->data, src->data, sizeof(dst->data));
  52. memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
  53. dst->pts = src->pts;
  54. dst->format = src->format;
  55. switch (src->type) {
  56. case AVMEDIA_TYPE_VIDEO:
  57. dst->width = src->video->w;
  58. dst->height = src->video->h;
  59. dst->sample_aspect_ratio = src->video->sample_aspect_ratio;
  60. dst->interlaced_frame = src->video->interlaced;
  61. dst->top_field_first = src->video->top_field_first;
  62. dst->key_frame = src->video->key_frame;
  63. dst->pict_type = src->video->pict_type;
  64. break;
  65. case AVMEDIA_TYPE_AUDIO:
  66. nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
  67. planes = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
  68. if (planes > FF_ARRAY_ELEMS(dst->data)) {
  69. dst->extended_data = av_mallocz(planes * sizeof(*dst->extended_data));
  70. if (!dst->extended_data)
  71. return AVERROR(ENOMEM);
  72. memcpy(dst->extended_data, src->extended_data,
  73. planes * sizeof(dst->extended_data));
  74. } else
  75. dst->extended_data = dst->data;
  76. dst->sample_rate = src->audio->sample_rate;
  77. dst->channel_layout = src->audio->channel_layout;
  78. dst->nb_samples = src->audio->nb_samples;
  79. break;
  80. default:
  81. return AVERROR(EINVAL);
  82. }
  83. return 0;
  84. }
  85. AVFilterBufferRef *avfilter_get_video_buffer_ref_from_frame(const AVFrame *frame,
  86. int perms)
  87. {
  88. AVFilterBufferRef *picref =
  89. avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize, perms,
  90. frame->width, frame->height,
  91. frame->format);
  92. if (!picref)
  93. return NULL;
  94. avfilter_copy_frame_props(picref, frame);
  95. return picref;
  96. }
  97. AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_frame(const AVFrame *frame,
  98. int perms)
  99. {
  100. AVFilterBufferRef *picref =
  101. avfilter_get_audio_buffer_ref_from_arrays((uint8_t **)frame->data, frame->linesize[0], perms,
  102. frame->nb_samples, frame->format,
  103. av_frame_get_channel_layout(frame));
  104. if (!picref)
  105. return NULL;
  106. avfilter_copy_frame_props(picref, frame);
  107. return picref;
  108. }
  109. int avfilter_fill_frame_from_audio_buffer_ref(AVFrame *frame,
  110. const AVFilterBufferRef *samplesref)
  111. {
  112. if (!samplesref || !samplesref->audio || !frame)
  113. return AVERROR(EINVAL);
  114. memcpy(frame->data, samplesref->data, sizeof(frame->data));
  115. frame->pkt_pos = samplesref->pos;
  116. frame->format = samplesref->format;
  117. frame->nb_samples = samplesref->audio->nb_samples;
  118. frame->pts = samplesref->pts;
  119. return 0;
  120. }
  121. int avfilter_fill_frame_from_video_buffer_ref(AVFrame *frame,
  122. const AVFilterBufferRef *picref)
  123. {
  124. if (!picref || !picref->video || !frame)
  125. return AVERROR(EINVAL);
  126. memcpy(frame->data, picref->data, sizeof(frame->data));
  127. memcpy(frame->linesize, picref->linesize, sizeof(frame->linesize));
  128. frame->pkt_pos = picref->pos;
  129. frame->interlaced_frame = picref->video->interlaced;
  130. frame->top_field_first = picref->video->top_field_first;
  131. frame->key_frame = picref->video->key_frame;
  132. frame->pict_type = picref->video->pict_type;
  133. frame->sample_aspect_ratio = picref->video->sample_aspect_ratio;
  134. frame->width = picref->video->w;
  135. frame->height = picref->video->h;
  136. frame->format = picref->format;
  137. frame->pts = picref->pts;
  138. return 0;
  139. }
  140. int avfilter_fill_frame_from_buffer_ref(AVFrame *frame,
  141. const AVFilterBufferRef *ref)
  142. {
  143. if (!ref)
  144. return AVERROR(EINVAL);
  145. return ref->video ? avfilter_fill_frame_from_video_buffer_ref(frame, ref)
  146. : avfilter_fill_frame_from_audio_buffer_ref(frame, ref);
  147. }