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.

182 lines
5.8KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/channel_layout.h"
  19. #include "libavutil/common.h"
  20. #include "libavutil/internal.h"
  21. #include "libavcodec/avcodec.h"
  22. #include "avfilter.h"
  23. #include "internal.h"
  24. #include "version.h"
  25. #if FF_API_AVFILTERBUFFER
  26. /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
  27. void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
  28. {
  29. if (ptr->extended_data != ptr->data)
  30. av_freep(&ptr->extended_data);
  31. av_free(ptr->data[0]);
  32. av_free(ptr);
  33. }
  34. AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
  35. {
  36. AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
  37. if (!ret)
  38. return NULL;
  39. *ret = *ref;
  40. if (ref->type == AVMEDIA_TYPE_VIDEO) {
  41. ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
  42. if (!ret->video) {
  43. av_free(ret);
  44. return NULL;
  45. }
  46. *ret->video = *ref->video;
  47. ret->extended_data = ret->data;
  48. } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
  49. ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
  50. if (!ret->audio) {
  51. av_free(ret);
  52. return NULL;
  53. }
  54. *ret->audio = *ref->audio;
  55. if (ref->extended_data != ref->data) {
  56. int nb_channels = av_get_channel_layout_nb_channels(ref->audio->channel_layout);
  57. if (!(ret->extended_data = av_malloc(sizeof(*ret->extended_data) *
  58. nb_channels))) {
  59. av_freep(&ret->audio);
  60. av_freep(&ret);
  61. return NULL;
  62. }
  63. memcpy(ret->extended_data, ref->extended_data,
  64. sizeof(*ret->extended_data) * nb_channels);
  65. } else
  66. ret->extended_data = ret->data;
  67. }
  68. ret->perms &= pmask;
  69. ret->buf->refcount ++;
  70. return ret;
  71. }
  72. void avfilter_unref_buffer(AVFilterBufferRef *ref)
  73. {
  74. if (!ref)
  75. return;
  76. if (!(--ref->buf->refcount))
  77. ref->buf->free(ref->buf);
  78. if (ref->extended_data != ref->data)
  79. av_freep(&ref->extended_data);
  80. av_free(ref->video);
  81. av_free(ref->audio);
  82. av_free(ref);
  83. }
  84. void avfilter_unref_bufferp(AVFilterBufferRef **ref)
  85. {
  86. FF_DISABLE_DEPRECATION_WARNINGS
  87. avfilter_unref_buffer(*ref);
  88. FF_ENABLE_DEPRECATION_WARNINGS
  89. *ref = NULL;
  90. }
  91. int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
  92. {
  93. dst->pts = src->pts;
  94. dst->format = src->format;
  95. switch (dst->type) {
  96. case AVMEDIA_TYPE_VIDEO:
  97. dst->video->w = src->width;
  98. dst->video->h = src->height;
  99. dst->video->pixel_aspect = src->sample_aspect_ratio;
  100. dst->video->interlaced = src->interlaced_frame;
  101. dst->video->top_field_first = src->top_field_first;
  102. dst->video->key_frame = src->key_frame;
  103. dst->video->pict_type = src->pict_type;
  104. break;
  105. case AVMEDIA_TYPE_AUDIO:
  106. dst->audio->sample_rate = src->sample_rate;
  107. dst->audio->channel_layout = src->channel_layout;
  108. break;
  109. default:
  110. return AVERROR(EINVAL);
  111. }
  112. return 0;
  113. }
  114. int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
  115. {
  116. int planes, nb_channels;
  117. memcpy(dst->data, src->data, sizeof(dst->data));
  118. memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
  119. dst->pts = src->pts;
  120. dst->format = src->format;
  121. switch (src->type) {
  122. case AVMEDIA_TYPE_VIDEO:
  123. dst->width = src->video->w;
  124. dst->height = src->video->h;
  125. dst->sample_aspect_ratio = src->video->pixel_aspect;
  126. dst->interlaced_frame = src->video->interlaced;
  127. dst->top_field_first = src->video->top_field_first;
  128. dst->key_frame = src->video->key_frame;
  129. dst->pict_type = src->video->pict_type;
  130. break;
  131. case AVMEDIA_TYPE_AUDIO:
  132. nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
  133. planes = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
  134. if (planes > FF_ARRAY_ELEMS(dst->data)) {
  135. dst->extended_data = av_mallocz(planes * sizeof(*dst->extended_data));
  136. if (!dst->extended_data)
  137. return AVERROR(ENOMEM);
  138. memcpy(dst->extended_data, src->extended_data,
  139. planes * sizeof(*dst->extended_data));
  140. } else
  141. dst->extended_data = dst->data;
  142. dst->sample_rate = src->audio->sample_rate;
  143. dst->channel_layout = src->audio->channel_layout;
  144. dst->nb_samples = src->audio->nb_samples;
  145. break;
  146. default:
  147. return AVERROR(EINVAL);
  148. }
  149. return 0;
  150. }
  151. void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
  152. {
  153. // copy common properties
  154. dst->pts = src->pts;
  155. dst->pos = src->pos;
  156. switch (src->type) {
  157. case AVMEDIA_TYPE_VIDEO: *dst->video = *src->video; break;
  158. case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
  159. default: break;
  160. }
  161. }
  162. #endif /* FF_API_AVFILTERBUFFER */