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.

176 lines
5.6KB

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