buffer.c 7.0 KB

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