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.

217 lines
6.2KB

  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/audioconvert.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/imgutils.h"
  25. #include "libavcodec/avcodec.h"
  26. #include "avfilter.h"
  27. #include "internal.h"
  28. #include "audio.h"
  29. #include "avcodec.h"
  30. void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
  31. {
  32. if (ptr->extended_data != ptr->data)
  33. av_freep(&ptr->extended_data);
  34. av_free(ptr->data[0]);
  35. av_free(ptr);
  36. }
  37. AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
  38. {
  39. AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
  40. if (!ret)
  41. return NULL;
  42. *ret = *ref;
  43. if (ref->type == AVMEDIA_TYPE_VIDEO) {
  44. ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
  45. if (!ret->video) {
  46. av_free(ret);
  47. return NULL;
  48. }
  49. *ret->video = *ref->video;
  50. ret->extended_data = ret->data;
  51. } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
  52. ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
  53. if (!ret->audio) {
  54. av_free(ret);
  55. return NULL;
  56. }
  57. *ret->audio = *ref->audio;
  58. if (ref->extended_data && ref->extended_data != ref->data) {
  59. int nb_channels = av_get_channel_layout_nb_channels(ref->audio->channel_layout);
  60. if (!(ret->extended_data = av_malloc(sizeof(*ret->extended_data) *
  61. nb_channels))) {
  62. av_freep(&ret->audio);
  63. av_freep(&ret);
  64. return NULL;
  65. }
  66. memcpy(ret->extended_data, ref->extended_data,
  67. sizeof(*ret->extended_data) * nb_channels);
  68. } else
  69. ret->extended_data = ret->data;
  70. }
  71. ret->perms &= pmask;
  72. ret->buf->refcount ++;
  73. return ret;
  74. }
  75. void ff_free_pool(AVFilterPool *pool)
  76. {
  77. int i;
  78. av_assert0(pool->refcount > 0);
  79. for (i = 0; i < POOL_SIZE; i++) {
  80. if (pool->pic[i]) {
  81. AVFilterBufferRef *picref = pool->pic[i];
  82. /* free buffer: picrefs stored in the pool are not
  83. * supposed to contain a free callback */
  84. av_assert0(!picref->buf->refcount);
  85. av_freep(&picref->buf->data[0]);
  86. av_freep(&picref->buf);
  87. av_freep(&picref->audio);
  88. av_freep(&picref->video);
  89. av_freep(&pool->pic[i]);
  90. pool->count--;
  91. }
  92. }
  93. pool->draining = 1;
  94. if (!--pool->refcount) {
  95. av_assert0(!pool->count);
  96. av_free(pool);
  97. }
  98. }
  99. static void store_in_pool(AVFilterBufferRef *ref)
  100. {
  101. int i;
  102. AVFilterPool *pool= ref->buf->priv;
  103. av_assert0(ref->buf->data[0]);
  104. av_assert0(pool->refcount>0);
  105. if (pool->count == POOL_SIZE) {
  106. AVFilterBufferRef *ref1 = pool->pic[0];
  107. av_freep(&ref1->video);
  108. av_freep(&ref1->audio);
  109. av_freep(&ref1->buf->data[0]);
  110. av_freep(&ref1->buf);
  111. av_free(ref1);
  112. memmove(&pool->pic[0], &pool->pic[1], sizeof(void*)*(POOL_SIZE-1));
  113. pool->count--;
  114. pool->pic[POOL_SIZE-1] = NULL;
  115. }
  116. for (i = 0; i < POOL_SIZE; i++) {
  117. if (!pool->pic[i]) {
  118. pool->pic[i] = ref;
  119. pool->count++;
  120. break;
  121. }
  122. }
  123. if (pool->draining) {
  124. ff_free_pool(pool);
  125. } else
  126. --pool->refcount;
  127. }
  128. void avfilter_unref_buffer(AVFilterBufferRef *ref)
  129. {
  130. if (!ref)
  131. return;
  132. av_assert0(ref->buf->refcount > 0);
  133. if (!(--ref->buf->refcount)) {
  134. if (!ref->buf->free) {
  135. store_in_pool(ref);
  136. return;
  137. }
  138. ref->buf->free(ref->buf);
  139. }
  140. if (ref->extended_data != ref->data)
  141. av_freep(&ref->extended_data);
  142. av_freep(&ref->video);
  143. av_freep(&ref->audio);
  144. av_free(ref);
  145. }
  146. void avfilter_unref_bufferp(AVFilterBufferRef **ref)
  147. {
  148. avfilter_unref_buffer(*ref);
  149. *ref = NULL;
  150. }
  151. void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
  152. {
  153. // copy common properties
  154. dst->pts = src->pts;
  155. dst->pos = src->pos;
  156. switch (src->type) {
  157. case AVMEDIA_TYPE_VIDEO: *dst->video = *src->video; break;
  158. case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
  159. default: break;
  160. }
  161. }
  162. AVFilterBufferRef *ff_copy_buffer_ref(AVFilterLink *outlink,
  163. AVFilterBufferRef *ref)
  164. {
  165. AVFilterBufferRef *buf;
  166. int channels;
  167. switch (outlink->type) {
  168. case AVMEDIA_TYPE_VIDEO:
  169. buf = ff_get_video_buffer(outlink, AV_PERM_WRITE,
  170. ref->video->w, ref->video->h);
  171. if(!buf)
  172. return NULL;
  173. av_image_copy(buf->data, buf->linesize,
  174. (void*)ref->data, ref->linesize,
  175. ref->format, ref->video->w, ref->video->h);
  176. break;
  177. case AVMEDIA_TYPE_AUDIO:
  178. buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE,
  179. ref->audio->nb_samples);
  180. if(!buf)
  181. return NULL;
  182. channels = av_get_channel_layout_nb_channels(ref->audio->channel_layout);
  183. av_samples_copy(buf->extended_data, ref->buf->extended_data,
  184. 0, 0, ref->audio->nb_samples,
  185. channels,
  186. ref->format);
  187. break;
  188. default:
  189. return NULL;
  190. }
  191. avfilter_copy_buffer_ref_props(buf, ref);
  192. return buf;
  193. }