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.

302 lines
10.0KB

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