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.

174 lines
6.3KB

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