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.

168 lines
5.1KB

  1. /*
  2. * Copyright Stefano Sabatini <stefasab gmail com>
  3. * Copyright Anton Khirnov <anton khirnov net>
  4. * Copyright Michael Niedermayer <michaelni gmx at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/channel_layout.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/imgutils.h"
  26. #include "libavcodec/avcodec.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. #include "audio.h"
  30. #include "avcodec.h"
  31. void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
  32. {
  33. if (ptr->extended_data != ptr->data)
  34. av_freep(&ptr->extended_data);
  35. av_free(ptr->data[0]);
  36. av_free(ptr);
  37. }
  38. static void copy_video_props(AVFilterBufferRefVideoProps *dst, AVFilterBufferRefVideoProps *src) {
  39. *dst = *src;
  40. if (src->qp_table) {
  41. int qsize = src->qp_table_size;
  42. dst->qp_table = av_malloc(qsize);
  43. memcpy(dst->qp_table, src->qp_table, qsize);
  44. }
  45. }
  46. AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
  47. {
  48. AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
  49. if (!ret)
  50. return NULL;
  51. *ret = *ref;
  52. ret->metadata = NULL;
  53. av_dict_copy(&ret->metadata, ref->metadata, 0);
  54. if (ref->type == AVMEDIA_TYPE_VIDEO) {
  55. ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
  56. if (!ret->video) {
  57. av_free(ret);
  58. return NULL;
  59. }
  60. copy_video_props(ret->video, ref->video);
  61. ret->extended_data = ret->data;
  62. } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
  63. ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
  64. if (!ret->audio) {
  65. av_free(ret);
  66. return NULL;
  67. }
  68. *ret->audio = *ref->audio;
  69. if (ref->extended_data && ref->extended_data != ref->data) {
  70. int nb_channels = av_get_channel_layout_nb_channels(ref->audio->channel_layout);
  71. if (!(ret->extended_data = av_malloc(sizeof(*ret->extended_data) *
  72. nb_channels))) {
  73. av_freep(&ret->audio);
  74. av_freep(&ret);
  75. return NULL;
  76. }
  77. memcpy(ret->extended_data, ref->extended_data,
  78. sizeof(*ret->extended_data) * nb_channels);
  79. } else
  80. ret->extended_data = ret->data;
  81. }
  82. ret->perms &= pmask;
  83. ret->buf->refcount ++;
  84. return ret;
  85. }
  86. void avfilter_unref_buffer(AVFilterBufferRef *ref)
  87. {
  88. if (!ref)
  89. return;
  90. av_assert0(ref->buf->refcount > 0);
  91. if (!(--ref->buf->refcount))
  92. ref->buf->free(ref->buf);
  93. if (ref->extended_data != ref->data)
  94. av_freep(&ref->extended_data);
  95. if (ref->video)
  96. av_freep(&ref->video->qp_table);
  97. av_freep(&ref->video);
  98. av_freep(&ref->audio);
  99. av_dict_free(&ref->metadata);
  100. av_free(ref);
  101. }
  102. void avfilter_unref_bufferp(AVFilterBufferRef **ref)
  103. {
  104. avfilter_unref_buffer(*ref);
  105. *ref = NULL;
  106. }
  107. int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
  108. {
  109. dst->pts = src->pts;
  110. dst->pos = av_frame_get_pkt_pos(src);
  111. dst->format = src->format;
  112. av_dict_free(&dst->metadata);
  113. av_dict_copy(&dst->metadata, av_frame_get_metadata(src), 0);
  114. switch (dst->type) {
  115. case AVMEDIA_TYPE_VIDEO:
  116. dst->video->w = src->width;
  117. dst->video->h = src->height;
  118. dst->video->sample_aspect_ratio = src->sample_aspect_ratio;
  119. dst->video->interlaced = src->interlaced_frame;
  120. dst->video->top_field_first = src->top_field_first;
  121. dst->video->key_frame = src->key_frame;
  122. dst->video->pict_type = src->pict_type;
  123. break;
  124. case AVMEDIA_TYPE_AUDIO:
  125. dst->audio->sample_rate = src->sample_rate;
  126. dst->audio->channel_layout = src->channel_layout;
  127. break;
  128. default:
  129. return AVERROR(EINVAL);
  130. }
  131. return 0;
  132. }
  133. void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
  134. {
  135. // copy common properties
  136. dst->pts = src->pts;
  137. dst->pos = src->pos;
  138. switch (src->type) {
  139. case AVMEDIA_TYPE_VIDEO: {
  140. if (dst->video->qp_table)
  141. av_freep(&dst->video->qp_table);
  142. copy_video_props(dst->video, src->video);
  143. break;
  144. }
  145. case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
  146. default: break;
  147. }
  148. av_dict_free(&dst->metadata);
  149. av_dict_copy(&dst->metadata, src->metadata, 0);
  150. }