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.

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