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.

310 lines
10KB

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