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.

245 lines
7.3KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/audioconvert.h"
  19. #include "libavutil/avassert.h"
  20. #include "libavcodec/avcodec.h"
  21. #include "avfilter.h"
  22. #include "internal.h"
  23. #include "avcodec.h"
  24. void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
  25. {
  26. if (ptr->extended_data != ptr->data)
  27. av_freep(&ptr->extended_data);
  28. av_free(ptr->data[0]);
  29. av_free(ptr);
  30. }
  31. AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
  32. {
  33. AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
  34. if (!ret)
  35. return NULL;
  36. *ret = *ref;
  37. if (ref->type == AVMEDIA_TYPE_VIDEO) {
  38. ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
  39. if (!ret->video) {
  40. av_free(ret);
  41. return NULL;
  42. }
  43. *ret->video = *ref->video;
  44. ret->extended_data = ret->data;
  45. } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
  46. ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
  47. if (!ret->audio) {
  48. av_free(ret);
  49. return NULL;
  50. }
  51. *ret->audio = *ref->audio;
  52. if (ref->extended_data && ref->extended_data != ref->data) {
  53. int nb_channels = av_get_channel_layout_nb_channels(ref->audio->channel_layout);
  54. if (!(ret->extended_data = av_malloc(sizeof(*ret->extended_data) *
  55. nb_channels))) {
  56. av_freep(&ret->audio);
  57. av_freep(&ret);
  58. return NULL;
  59. }
  60. memcpy(ret->extended_data, ref->extended_data,
  61. sizeof(*ret->extended_data) * nb_channels);
  62. } else
  63. ret->extended_data = ret->data;
  64. }
  65. ret->perms &= pmask;
  66. ret->buf->refcount ++;
  67. return ret;
  68. }
  69. void ff_free_pool(AVFilterPool *pool)
  70. {
  71. int i;
  72. av_assert0(pool->refcount > 0);
  73. for (i = 0; i < POOL_SIZE; i++) {
  74. if (pool->pic[i]) {
  75. AVFilterBufferRef *picref = pool->pic[i];
  76. /* free buffer: picrefs stored in the pool are not
  77. * supposed to contain a free callback */
  78. av_assert0(!picref->buf->refcount);
  79. av_freep(&picref->buf->data[0]);
  80. av_freep(&picref->buf);
  81. av_freep(&picref->audio);
  82. av_freep(&picref->video);
  83. av_freep(&pool->pic[i]);
  84. pool->count--;
  85. }
  86. }
  87. pool->draining = 1;
  88. if (!--pool->refcount) {
  89. av_assert0(!pool->count);
  90. av_free(pool);
  91. }
  92. }
  93. static void store_in_pool(AVFilterBufferRef *ref)
  94. {
  95. int i;
  96. AVFilterPool *pool= ref->buf->priv;
  97. av_assert0(ref->buf->data[0]);
  98. av_assert0(pool->refcount>0);
  99. if (pool->count == POOL_SIZE) {
  100. AVFilterBufferRef *ref1 = pool->pic[0];
  101. av_freep(&ref1->video);
  102. av_freep(&ref1->audio);
  103. av_freep(&ref1->buf->data[0]);
  104. av_freep(&ref1->buf);
  105. av_free(ref1);
  106. memmove(&pool->pic[0], &pool->pic[1], sizeof(void*)*(POOL_SIZE-1));
  107. pool->count--;
  108. pool->pic[POOL_SIZE-1] = NULL;
  109. }
  110. for (i = 0; i < POOL_SIZE; i++) {
  111. if (!pool->pic[i]) {
  112. pool->pic[i] = ref;
  113. pool->count++;
  114. break;
  115. }
  116. }
  117. if (pool->draining) {
  118. ff_free_pool(pool);
  119. } else
  120. --pool->refcount;
  121. }
  122. void avfilter_unref_buffer(AVFilterBufferRef *ref)
  123. {
  124. if (!ref)
  125. return;
  126. av_assert0(ref->buf->refcount > 0);
  127. if (!(--ref->buf->refcount)) {
  128. if (!ref->buf->free) {
  129. store_in_pool(ref);
  130. return;
  131. }
  132. ref->buf->free(ref->buf);
  133. }
  134. if (ref->extended_data != ref->data)
  135. av_freep(&ref->extended_data);
  136. av_freep(&ref->video);
  137. av_freep(&ref->audio);
  138. av_free(ref);
  139. }
  140. void avfilter_unref_bufferp(AVFilterBufferRef **ref)
  141. {
  142. avfilter_unref_buffer(*ref);
  143. *ref = NULL;
  144. }
  145. int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
  146. {
  147. dst->pts = src->pts;
  148. dst->pos = src->pkt_pos;
  149. dst->format = src->format;
  150. switch (dst->type) {
  151. case AVMEDIA_TYPE_VIDEO:
  152. dst->video->w = src->width;
  153. dst->video->h = src->height;
  154. dst->video->sample_aspect_ratio = src->sample_aspect_ratio;
  155. dst->video->interlaced = src->interlaced_frame;
  156. dst->video->top_field_first = src->top_field_first;
  157. dst->video->key_frame = src->key_frame;
  158. dst->video->pict_type = src->pict_type;
  159. break;
  160. case AVMEDIA_TYPE_AUDIO:
  161. dst->audio->sample_rate = src->sample_rate;
  162. dst->audio->channel_layout = src->channel_layout;
  163. break;
  164. default:
  165. return AVERROR(EINVAL);
  166. }
  167. return 0;
  168. }
  169. int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
  170. {
  171. int planes, nb_channels;
  172. memcpy(dst->data, src->data, sizeof(dst->data));
  173. memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
  174. dst->pts = src->pts;
  175. dst->format = src->format;
  176. switch (src->type) {
  177. case AVMEDIA_TYPE_VIDEO:
  178. dst->width = src->video->w;
  179. dst->height = src->video->h;
  180. dst->sample_aspect_ratio = src->video->sample_aspect_ratio;
  181. dst->interlaced_frame = src->video->interlaced;
  182. dst->top_field_first = src->video->top_field_first;
  183. dst->key_frame = src->video->key_frame;
  184. dst->pict_type = src->video->pict_type;
  185. break;
  186. case AVMEDIA_TYPE_AUDIO:
  187. nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
  188. planes = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
  189. if (planes > FF_ARRAY_ELEMS(dst->data)) {
  190. dst->extended_data = av_mallocz(planes * sizeof(*dst->extended_data));
  191. if (!dst->extended_data)
  192. return AVERROR(ENOMEM);
  193. memcpy(dst->extended_data, src->extended_data,
  194. planes * sizeof(dst->extended_data));
  195. } else
  196. dst->extended_data = dst->data;
  197. dst->sample_rate = src->audio->sample_rate;
  198. dst->channel_layout = src->audio->channel_layout;
  199. dst->nb_samples = src->audio->nb_samples;
  200. break;
  201. default:
  202. return AVERROR(EINVAL);
  203. }
  204. return 0;
  205. }
  206. void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
  207. {
  208. // copy common properties
  209. dst->pts = src->pts;
  210. dst->pos = src->pos;
  211. switch (src->type) {
  212. case AVMEDIA_TYPE_VIDEO: *dst->video = *src->video; break;
  213. case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
  214. default: break;
  215. }
  216. }