buffer.c 6.8 KB

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