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.

175 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/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. void avfilter_unref_bufferp(AVFilterBufferRef **ref)
  81. {
  82. avfilter_unref_buffer(*ref);
  83. *ref = NULL;
  84. }
  85. int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
  86. {
  87. dst->pts = src->pts;
  88. dst->format = src->format;
  89. switch (dst->type) {
  90. case AVMEDIA_TYPE_VIDEO:
  91. dst->video->w = src->width;
  92. dst->video->h = src->height;
  93. dst->video->pixel_aspect = src->sample_aspect_ratio;
  94. dst->video->interlaced = src->interlaced_frame;
  95. dst->video->top_field_first = src->top_field_first;
  96. dst->video->key_frame = src->key_frame;
  97. dst->video->pict_type = src->pict_type;
  98. break;
  99. case AVMEDIA_TYPE_AUDIO:
  100. dst->audio->sample_rate = src->sample_rate;
  101. dst->audio->channel_layout = src->channel_layout;
  102. break;
  103. default:
  104. return AVERROR(EINVAL);
  105. }
  106. return 0;
  107. }
  108. int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
  109. {
  110. int planes, nb_channels;
  111. memcpy(dst->data, src->data, sizeof(dst->data));
  112. memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
  113. dst->pts = src->pts;
  114. dst->format = src->format;
  115. switch (src->type) {
  116. case AVMEDIA_TYPE_VIDEO:
  117. dst->width = src->video->w;
  118. dst->height = src->video->h;
  119. dst->sample_aspect_ratio = src->video->pixel_aspect;
  120. dst->interlaced_frame = src->video->interlaced;
  121. dst->top_field_first = src->video->top_field_first;
  122. dst->key_frame = src->video->key_frame;
  123. dst->pict_type = src->video->pict_type;
  124. break;
  125. case AVMEDIA_TYPE_AUDIO:
  126. nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
  127. planes = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
  128. if (planes > FF_ARRAY_ELEMS(dst->data)) {
  129. dst->extended_data = av_mallocz(planes * sizeof(*dst->extended_data));
  130. if (!dst->extended_data)
  131. return AVERROR(ENOMEM);
  132. memcpy(dst->extended_data, src->extended_data,
  133. planes * sizeof(dst->extended_data));
  134. } else
  135. dst->extended_data = dst->data;
  136. dst->sample_rate = src->audio->sample_rate;
  137. dst->channel_layout = src->audio->channel_layout;
  138. dst->nb_samples = src->audio->nb_samples;
  139. break;
  140. default:
  141. return AVERROR(EINVAL);
  142. }
  143. return 0;
  144. }
  145. void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
  146. {
  147. // copy common properties
  148. dst->pts = src->pts;
  149. dst->pos = src->pos;
  150. switch (src->type) {
  151. case AVMEDIA_TYPE_VIDEO: *dst->video = *src->video; break;
  152. case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
  153. default: break;
  154. }
  155. }