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.

179 lines
5.7KB

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