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.

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