defaults.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 "libavcore/samplefmt.h"
  23. #include "libavcodec/audioconvert.h"
  24. #include "avfilter.h"
  25. /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
  26. static void 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. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  37. AVFilterBufferRef *ref = NULL;
  38. int i, tempsize;
  39. char *buf = NULL;
  40. if (!pic || !(ref = av_mallocz(sizeof(AVFilterBufferRef))))
  41. goto fail;
  42. ref->buf = pic;
  43. ref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps));
  44. ref->video->w = w;
  45. ref->video->h = h;
  46. /* make sure the buffer gets read permission or it's useless for output */
  47. ref->perms = perms | AV_PERM_READ;
  48. pic->refcount = 1;
  49. ref->format = link->format;
  50. pic->free = avfilter_default_free_buffer;
  51. av_image_fill_linesizes(pic->linesize, ref->format, ref->video->w);
  52. for (i = 0; i < 4; i++)
  53. pic->linesize[i] = FFALIGN(pic->linesize[i], 16);
  54. tempsize = av_image_fill_pointers(pic->data, ref->format, ref->video->h, NULL, pic->linesize);
  55. buf = av_malloc(tempsize + 16); // +2 is needed for swscaler, +16 to be
  56. // SIMD-friendly
  57. if (!buf)
  58. goto fail;
  59. av_image_fill_pointers(pic->data, ref->format, ref->video->h, buf, pic->linesize);
  60. memcpy(ref->data, pic->data, sizeof(ref->data));
  61. memcpy(ref->linesize, pic->linesize, sizeof(ref->linesize));
  62. return ref;
  63. fail:
  64. av_free(buf);
  65. if (ref && ref->video)
  66. av_free(ref->video);
  67. av_free(ref);
  68. av_free(pic);
  69. return NULL;
  70. }
  71. AVFilterBufferRef *avfilter_default_get_audio_buffer(AVFilterLink *link, int perms,
  72. enum SampleFormat 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 = avfilter_default_free_buffer;
  93. sample_size = av_get_bits_per_sample_fmt(sample_fmt) >>3;
  94. chans_nb = avcodec_channel_layout_num_channels(channel_layout);
  95. per_channel_size = size/chans_nb;
  96. ref->audio->samples_nb = 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. av_free(buf);
  128. if (ref && ref->audio)
  129. av_free(ref->audio);
  130. av_free(ref);
  131. av_free(samples);
  132. return NULL;
  133. }
  134. void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  135. {
  136. AVFilterLink *outlink = NULL;
  137. if (inlink->dst->output_count)
  138. outlink = inlink->dst->outputs[0];
  139. if (outlink) {
  140. outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  141. avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
  142. avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
  143. }
  144. }
  145. void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  146. {
  147. AVFilterLink *outlink = NULL;
  148. if (inlink->dst->output_count)
  149. outlink = inlink->dst->outputs[0];
  150. if (outlink)
  151. avfilter_draw_slice(outlink, y, h, slice_dir);
  152. }
  153. void avfilter_default_end_frame(AVFilterLink *inlink)
  154. {
  155. AVFilterLink *outlink = NULL;
  156. if (inlink->dst->output_count)
  157. outlink = inlink->dst->outputs[0];
  158. avfilter_unref_buffer(inlink->cur_buf);
  159. inlink->cur_buf = NULL;
  160. if (outlink) {
  161. if (outlink->out_buf) {
  162. avfilter_unref_buffer(outlink->out_buf);
  163. outlink->out_buf = NULL;
  164. }
  165. avfilter_end_frame(outlink);
  166. }
  167. }
  168. /* FIXME: samplesref is same as link->cur_buf. Need to consider removing the redundant parameter. */
  169. void avfilter_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
  170. {
  171. AVFilterLink *outlink = NULL;
  172. if (inlink->dst->output_count)
  173. outlink = inlink->dst->outputs[0];
  174. if (outlink) {
  175. outlink->out_buf = avfilter_default_get_audio_buffer(inlink, AV_PERM_WRITE, samplesref->format,
  176. samplesref->audio->size,
  177. samplesref->audio->channel_layout,
  178. samplesref->audio->planar);
  179. outlink->out_buf->pts = samplesref->pts;
  180. outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
  181. avfilter_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
  182. avfilter_unref_buffer(outlink->out_buf);
  183. outlink->out_buf = NULL;
  184. }
  185. avfilter_unref_buffer(samplesref);
  186. inlink->cur_buf = NULL;
  187. }
  188. /**
  189. * default config_link() implementation for output video links to simplify
  190. * the implementation of one input one output video filters */
  191. int avfilter_default_config_output_link(AVFilterLink *link)
  192. {
  193. if (link->src->input_count && link->src->inputs[0]) {
  194. if (link->type == AVMEDIA_TYPE_VIDEO) {
  195. link->w = link->src->inputs[0]->w;
  196. link->h = link->src->inputs[0]->h;
  197. link->time_base = link->src->inputs[0]->time_base;
  198. } else if (link->type == AVMEDIA_TYPE_AUDIO) {
  199. link->channel_layout = link->src->inputs[0]->channel_layout;
  200. link->sample_rate = link->src->inputs[0]->sample_rate;
  201. }
  202. } else {
  203. /* XXX: any non-simple filter which would cause this branch to be taken
  204. * really should implement its own config_props() for this link. */
  205. return -1;
  206. }
  207. return 0;
  208. }
  209. /**
  210. * A helper for query_formats() which sets all links to the same list of
  211. * formats. If there are no links hooked to this filter, the list of formats is
  212. * freed.
  213. *
  214. * FIXME: this will need changed for filters with a mix of pad types
  215. * (video + audio, etc)
  216. */
  217. void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  218. {
  219. int count = 0, i;
  220. for (i = 0; i < ctx->input_count; i++) {
  221. if (ctx->inputs[i]) {
  222. avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
  223. count++;
  224. }
  225. }
  226. for (i = 0; i < ctx->output_count; i++) {
  227. if (ctx->outputs[i]) {
  228. avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
  229. count++;
  230. }
  231. }
  232. if (!count) {
  233. av_free(formats->formats);
  234. av_free(formats->refs);
  235. av_free(formats);
  236. }
  237. }
  238. int avfilter_default_query_formats(AVFilterContext *ctx)
  239. {
  240. enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
  241. ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
  242. AVMEDIA_TYPE_VIDEO;
  243. avfilter_set_common_formats(ctx, avfilter_all_formats(type));
  244. return 0;
  245. }
  246. void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  247. {
  248. avfilter_start_frame(link->dst->outputs[0], picref);
  249. }
  250. void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  251. {
  252. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  253. }
  254. void avfilter_null_end_frame(AVFilterLink *link)
  255. {
  256. avfilter_end_frame(link->dst->outputs[0]);
  257. }
  258. void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  259. {
  260. avfilter_filter_samples(link->dst->outputs[0], samplesref);
  261. }
  262. AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  263. {
  264. return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  265. }
  266. AVFilterBufferRef *avfilter_null_get_audio_buffer(AVFilterLink *link, int perms,
  267. enum SampleFormat sample_fmt, int size,
  268. int64_t channel_layout, int packed)
  269. {
  270. return avfilter_get_audio_buffer(link->dst->outputs[0], perms, sample_fmt,
  271. size, channel_layout, packed);
  272. }