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
5.5KB

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