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.

329 lines
11KB

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