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.

326 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/imgutils.h"
  22. #include "libavcodec/audioconvert.h"
  23. #include "avfilter.h"
  24. /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
  25. static void avfilter_default_free_buffer(AVFilterBuffer *ptr)
  26. {
  27. av_free(ptr->data[0]);
  28. av_free(ptr);
  29. }
  30. /* TODO: set the buffer's priv member to a context structure for the whole
  31. * filter chain. This will allow for a buffer pool instead of the constant
  32. * alloc & free cycle currently implemented. */
  33. AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  34. {
  35. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  36. AVFilterBufferRef *ref = NULL;
  37. int i, tempsize;
  38. char *buf = NULL;
  39. if (!pic || !(ref = av_mallocz(sizeof(AVFilterBufferRef))))
  40. goto fail;
  41. ref->buf = pic;
  42. ref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps));
  43. ref->video->w = w;
  44. ref->video->h = h;
  45. /* make sure the buffer gets read permission or it's useless for output */
  46. ref->perms = perms | AV_PERM_READ;
  47. pic->refcount = 1;
  48. ref->format = link->format;
  49. pic->free = avfilter_default_free_buffer;
  50. av_fill_image_linesizes(pic->linesize, ref->format, ref->video->w);
  51. for (i = 0; i < 4; i++)
  52. pic->linesize[i] = FFALIGN(pic->linesize[i], 16);
  53. tempsize = av_fill_image_pointers(pic->data, ref->format, ref->video->h, NULL, pic->linesize);
  54. buf = av_malloc(tempsize + 16); // +2 is needed for swscaler, +16 to be
  55. // SIMD-friendly
  56. if (!buf)
  57. goto fail;
  58. av_fill_image_pointers(pic->data, ref->format, ref->video->h, buf, pic->linesize);
  59. memcpy(ref->data, pic->data, sizeof(ref->data));
  60. memcpy(ref->linesize, pic->linesize, sizeof(ref->linesize));
  61. return ref;
  62. fail:
  63. av_free(buf);
  64. if (ref && ref->video)
  65. av_free(ref->video);
  66. av_free(ref);
  67. av_free(pic);
  68. return NULL;
  69. }
  70. AVFilterBufferRef *avfilter_default_get_audio_buffer(AVFilterLink *link, int perms,
  71. enum SampleFormat 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 = avfilter_default_free_buffer;
  92. sample_size = av_get_bits_per_sample_format(sample_fmt) >>3;
  93. chans_nb = avcodec_channel_layout_num_channels(channel_layout);
  94. per_channel_size = size/chans_nb;
  95. ref->audio->samples_nb = 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. av_free(buf);
  127. if (ref && ref->audio)
  128. av_free(ref->audio);
  129. av_free(ref);
  130. av_free(samples);
  131. return NULL;
  132. }
  133. void avfilter_default_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  134. {
  135. AVFilterLink *out = NULL;
  136. if (link->dst->output_count)
  137. out = link->dst->outputs[0];
  138. if (out) {
  139. out->out_buf = avfilter_get_video_buffer(out, AV_PERM_WRITE, out->w, out->h);
  140. avfilter_copy_buffer_ref_props(out->out_buf, picref);
  141. avfilter_start_frame(out, avfilter_ref_buffer(out->out_buf, ~0));
  142. }
  143. }
  144. void avfilter_default_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  145. {
  146. AVFilterLink *out = NULL;
  147. if (link->dst->output_count)
  148. out = link->dst->outputs[0];
  149. if (out)
  150. avfilter_draw_slice(out, y, h, slice_dir);
  151. }
  152. void avfilter_default_end_frame(AVFilterLink *link)
  153. {
  154. AVFilterLink *out = NULL;
  155. if (link->dst->output_count)
  156. out = link->dst->outputs[0];
  157. avfilter_unref_buffer(link->cur_buf);
  158. link->cur_buf = NULL;
  159. if (out) {
  160. if (out->out_buf) {
  161. avfilter_unref_buffer(out->out_buf);
  162. out->out_buf = NULL;
  163. }
  164. avfilter_end_frame(out);
  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 *link, AVFilterBufferRef *samplesref)
  169. {
  170. AVFilterLink *outlink = NULL;
  171. if (link->dst->output_count)
  172. outlink = link->dst->outputs[0];
  173. if (outlink) {
  174. outlink->out_buf = avfilter_default_get_audio_buffer(link, 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. link->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. } 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 [0] ? ctx->inputs [0]->type :
  239. 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 SampleFormat 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. }