buffer.c 7.5 KB

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