defaults.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Filter layer - default implementations
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/audioconvert.h"
  22. #include "libavutil/imgutils.h"
  23. #include "libavutil/samplefmt.h"
  24. #include "avfilter.h"
  25. #include "internal.h"
  26. /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
  27. void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
  28. {
  29. av_free(ptr->data[0]);
  30. av_free(ptr);
  31. }
  32. /* TODO: set the buffer's priv member to a context structure for the whole
  33. * filter chain. This will allow for a buffer pool instead of the constant
  34. * alloc & free cycle currently implemented. */
  35. AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  36. {
  37. int linesize[4];
  38. uint8_t *data[4];
  39. AVFilterBufferRef *picref = NULL;
  40. // +2 is needed for swscaler, +16 to be SIMD-friendly
  41. if (av_image_alloc(data, linesize, w, h, link->format, 16) < 0)
  42. return NULL;
  43. picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
  44. perms, w, h, link->format);
  45. if (!picref) {
  46. av_free(data[0]);
  47. return NULL;
  48. }
  49. return picref;
  50. }
  51. AVFilterBufferRef *avfilter_default_get_audio_buffer(AVFilterLink *link, int perms,
  52. enum AVSampleFormat sample_fmt, int size,
  53. int64_t channel_layout, int planar)
  54. {
  55. AVFilterBuffer *samples = av_mallocz(sizeof(AVFilterBuffer));
  56. AVFilterBufferRef *ref = NULL;
  57. int i, sample_size, chans_nb, bufsize, per_channel_size, step_size = 0;
  58. char *buf;
  59. if (!samples || !(ref = av_mallocz(sizeof(AVFilterBufferRef))))
  60. goto fail;
  61. ref->buf = samples;
  62. ref->format = sample_fmt;
  63. ref->audio = av_mallocz(sizeof(AVFilterBufferRefAudioProps));
  64. if (!ref->audio)
  65. goto fail;
  66. ref->audio->channel_layout = channel_layout;
  67. ref->audio->size = size;
  68. ref->audio->planar = planar;
  69. /* make sure the buffer gets read permission or it's useless for output */
  70. ref->perms = perms | AV_PERM_READ;
  71. samples->refcount = 1;
  72. samples->free = ff_avfilter_default_free_buffer;
  73. sample_size = av_get_bytes_per_sample(sample_fmt);
  74. chans_nb = av_get_channel_layout_nb_channels(channel_layout);
  75. per_channel_size = size/chans_nb;
  76. ref->audio->nb_samples = per_channel_size/sample_size;
  77. /* Set the number of bytes to traverse to reach next sample of a particular channel:
  78. * For planar, this is simply the sample size.
  79. * For packed, this is the number of samples * sample_size.
  80. */
  81. for (i = 0; i < chans_nb; i++)
  82. samples->linesize[i] = planar > 0 ? per_channel_size : sample_size;
  83. memset(&samples->linesize[chans_nb], 0, (8-chans_nb) * sizeof(samples->linesize[0]));
  84. /* Calculate total buffer size, round to multiple of 16 to be SIMD friendly */
  85. bufsize = (size + 15)&~15;
  86. buf = av_malloc(bufsize);
  87. if (!buf)
  88. goto fail;
  89. /* For planar, set the start point of each channel's data within the buffer
  90. * For packed, set the start point of the entire buffer only
  91. */
  92. samples->data[0] = buf;
  93. if (buf && planar) {
  94. for (i = 1; i < chans_nb; i++) {
  95. step_size += per_channel_size;
  96. samples->data[i] = buf + step_size;
  97. }
  98. } else {
  99. for (i = 1; i < chans_nb; i++)
  100. samples->data[i] = buf;
  101. }
  102. memset(&samples->data[chans_nb], 0, (8-chans_nb) * sizeof(samples->data[0]));
  103. memcpy(ref->data, samples->data, sizeof(ref->data));
  104. memcpy(ref->linesize, samples->linesize, sizeof(ref->linesize));
  105. return ref;
  106. fail:
  107. if (ref)
  108. av_free(ref->audio);
  109. av_free(ref);
  110. av_free(samples);
  111. return NULL;
  112. }
  113. void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  114. {
  115. AVFilterLink *outlink = NULL;
  116. if (inlink->dst->output_count)
  117. outlink = inlink->dst->outputs[0];
  118. if (outlink) {
  119. outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  120. avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
  121. avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
  122. }
  123. }
  124. void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  125. {
  126. AVFilterLink *outlink = NULL;
  127. if (inlink->dst->output_count)
  128. outlink = inlink->dst->outputs[0];
  129. if (outlink)
  130. avfilter_draw_slice(outlink, y, h, slice_dir);
  131. }
  132. void avfilter_default_end_frame(AVFilterLink *inlink)
  133. {
  134. AVFilterLink *outlink = NULL;
  135. if (inlink->dst->output_count)
  136. outlink = inlink->dst->outputs[0];
  137. avfilter_unref_buffer(inlink->cur_buf);
  138. inlink->cur_buf = NULL;
  139. if (outlink) {
  140. if (outlink->out_buf) {
  141. avfilter_unref_buffer(outlink->out_buf);
  142. outlink->out_buf = NULL;
  143. }
  144. avfilter_end_frame(outlink);
  145. }
  146. }
  147. /* FIXME: samplesref is same as link->cur_buf. Need to consider removing the redundant parameter. */
  148. void avfilter_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
  149. {
  150. AVFilterLink *outlink = NULL;
  151. if (inlink->dst->output_count)
  152. outlink = inlink->dst->outputs[0];
  153. if (outlink) {
  154. outlink->out_buf = avfilter_default_get_audio_buffer(inlink, AV_PERM_WRITE, samplesref->format,
  155. samplesref->audio->size,
  156. samplesref->audio->channel_layout,
  157. samplesref->audio->planar);
  158. outlink->out_buf->pts = samplesref->pts;
  159. outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
  160. avfilter_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
  161. avfilter_unref_buffer(outlink->out_buf);
  162. outlink->out_buf = NULL;
  163. }
  164. avfilter_unref_buffer(samplesref);
  165. inlink->cur_buf = NULL;
  166. }
  167. /**
  168. * default config_link() implementation for output video links to simplify
  169. * the implementation of one input one output video filters */
  170. int avfilter_default_config_output_link(AVFilterLink *link)
  171. {
  172. if (link->src->input_count && link->src->inputs[0]) {
  173. if (link->type == AVMEDIA_TYPE_VIDEO) {
  174. link->w = link->src->inputs[0]->w;
  175. link->h = link->src->inputs[0]->h;
  176. link->time_base = link->src->inputs[0]->time_base;
  177. } else if (link->type == AVMEDIA_TYPE_AUDIO) {
  178. link->channel_layout = link->src->inputs[0]->channel_layout;
  179. link->sample_rate = link->src->inputs[0]->sample_rate;
  180. }
  181. } else {
  182. /* XXX: any non-simple filter which would cause this branch to be taken
  183. * really should implement its own config_props() for this link. */
  184. return -1;
  185. }
  186. return 0;
  187. }
  188. /**
  189. * A helper for query_formats() which sets all links to the same list of
  190. * formats. If there are no links hooked to this filter, the list of formats is
  191. * freed.
  192. *
  193. * FIXME: this will need changed for filters with a mix of pad types
  194. * (video + audio, etc)
  195. */
  196. void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  197. {
  198. int count = 0, i;
  199. for (i = 0; i < ctx->input_count; i++) {
  200. if (ctx->inputs[i]) {
  201. avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
  202. count++;
  203. }
  204. }
  205. for (i = 0; i < ctx->output_count; i++) {
  206. if (ctx->outputs[i]) {
  207. avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
  208. count++;
  209. }
  210. }
  211. if (!count) {
  212. av_free(formats->formats);
  213. av_free(formats->refs);
  214. av_free(formats);
  215. }
  216. }
  217. int avfilter_default_query_formats(AVFilterContext *ctx)
  218. {
  219. enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
  220. ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
  221. AVMEDIA_TYPE_VIDEO;
  222. avfilter_set_common_formats(ctx, avfilter_all_formats(type));
  223. return 0;
  224. }
  225. void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  226. {
  227. avfilter_start_frame(link->dst->outputs[0], picref);
  228. }
  229. void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  230. {
  231. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  232. }
  233. void avfilter_null_end_frame(AVFilterLink *link)
  234. {
  235. avfilter_end_frame(link->dst->outputs[0]);
  236. }
  237. void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  238. {
  239. avfilter_filter_samples(link->dst->outputs[0], samplesref);
  240. }
  241. AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  242. {
  243. return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  244. }
  245. AVFilterBufferRef *avfilter_null_get_audio_buffer(AVFilterLink *link, int perms,
  246. enum AVSampleFormat sample_fmt, int size,
  247. int64_t channel_layout, int packed)
  248. {
  249. return avfilter_get_audio_buffer(link->dst->outputs[0], perms, sample_fmt,
  250. size, channel_layout, packed);
  251. }