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.

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