buffer.c 5.5 KB

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