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.

295 lines
9.6KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/imgutils.h"
  19. #include "avfilter.h"
  20. #include "internal.h"
  21. static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
  22. {
  23. av_unused char buf[16];
  24. av_dlog(ctx,
  25. "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  26. ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
  27. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  28. ref->pts, ref->pos);
  29. if (ref->video) {
  30. av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
  31. ref->video->pixel_aspect.num, ref->video->pixel_aspect.den,
  32. ref->video->w, ref->video->h,
  33. !ref->video->interlaced ? 'P' : /* Progressive */
  34. ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
  35. ref->video->key_frame,
  36. av_get_picture_type_char(ref->video->pict_type));
  37. }
  38. if (ref->audio) {
  39. av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d p:%d",
  40. ref->audio->channel_layout,
  41. ref->audio->nb_samples,
  42. ref->audio->sample_rate,
  43. ref->audio->planar);
  44. }
  45. av_dlog(ctx, "]%s", end ? "\n" : "");
  46. }
  47. AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  48. {
  49. return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  50. }
  51. /* TODO: set the buffer's priv member to a context structure for the whole
  52. * filter chain. This will allow for a buffer pool instead of the constant
  53. * alloc & free cycle currently implemented. */
  54. AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  55. {
  56. int linesize[4];
  57. uint8_t *data[4];
  58. AVFilterBufferRef *picref = NULL;
  59. // +2 is needed for swscaler, +16 to be SIMD-friendly
  60. if (av_image_alloc(data, linesize, w, h, link->format, 16) < 0)
  61. return NULL;
  62. picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
  63. perms, w, h, link->format);
  64. if (!picref) {
  65. av_free(data[0]);
  66. return NULL;
  67. }
  68. return picref;
  69. }
  70. AVFilterBufferRef *
  71. avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms,
  72. int w, int h, enum PixelFormat format)
  73. {
  74. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  75. AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
  76. if (!pic || !picref)
  77. goto fail;
  78. picref->buf = pic;
  79. picref->buf->free = ff_avfilter_default_free_buffer;
  80. if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
  81. goto fail;
  82. pic->w = picref->video->w = w;
  83. pic->h = picref->video->h = h;
  84. /* make sure the buffer gets read permission or it's useless for output */
  85. picref->perms = perms | AV_PERM_READ;
  86. pic->refcount = 1;
  87. picref->type = AVMEDIA_TYPE_VIDEO;
  88. pic->format = picref->format = format;
  89. memcpy(pic->data, data, 4*sizeof(data[0]));
  90. memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
  91. memcpy(picref->data, pic->data, sizeof(picref->data));
  92. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  93. pic-> extended_data = pic->data;
  94. picref->extended_data = picref->data;
  95. return picref;
  96. fail:
  97. if (picref && picref->video)
  98. av_free(picref->video);
  99. av_free(picref);
  100. av_free(pic);
  101. return NULL;
  102. }
  103. AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  104. {
  105. AVFilterBufferRef *ret = NULL;
  106. av_unused char buf[16];
  107. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
  108. av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
  109. if (link->dstpad->get_video_buffer)
  110. ret = link->dstpad->get_video_buffer(link, perms, w, h);
  111. if (!ret)
  112. ret = avfilter_default_get_video_buffer(link, perms, w, h);
  113. if (ret)
  114. ret->type = AVMEDIA_TYPE_VIDEO;
  115. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
  116. return ret;
  117. }
  118. void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  119. {
  120. avfilter_start_frame(link->dst->outputs[0], picref);
  121. }
  122. void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  123. {
  124. AVFilterLink *outlink = NULL;
  125. if (inlink->dst->output_count)
  126. outlink = inlink->dst->outputs[0];
  127. if (outlink) {
  128. outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  129. avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
  130. avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
  131. }
  132. }
  133. /* XXX: should we do the duplicating of the picture ref here, instead of
  134. * forcing the source filter to do it? */
  135. void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  136. {
  137. void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
  138. AVFilterPad *dst = link->dstpad;
  139. int perms = picref->perms;
  140. FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
  141. if (!(start_frame = dst->start_frame))
  142. start_frame = avfilter_default_start_frame;
  143. if (picref->linesize[0] < 0)
  144. perms |= AV_PERM_NEG_LINESIZES;
  145. /* prepare to copy the picture if it has insufficient permissions */
  146. if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
  147. av_log(link->dst, AV_LOG_DEBUG,
  148. "frame copy needed (have perms %x, need %x, reject %x)\n",
  149. picref->perms,
  150. link->dstpad->min_perms, link->dstpad->rej_perms);
  151. link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
  152. link->src_buf = picref;
  153. avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
  154. }
  155. else
  156. link->cur_buf = picref;
  157. start_frame(link, link->cur_buf);
  158. }
  159. void avfilter_null_end_frame(AVFilterLink *link)
  160. {
  161. avfilter_end_frame(link->dst->outputs[0]);
  162. }
  163. void avfilter_default_end_frame(AVFilterLink *inlink)
  164. {
  165. AVFilterLink *outlink = NULL;
  166. if (inlink->dst->output_count)
  167. outlink = inlink->dst->outputs[0];
  168. avfilter_unref_buffer(inlink->cur_buf);
  169. inlink->cur_buf = NULL;
  170. if (outlink) {
  171. if (outlink->out_buf) {
  172. avfilter_unref_buffer(outlink->out_buf);
  173. outlink->out_buf = NULL;
  174. }
  175. avfilter_end_frame(outlink);
  176. }
  177. }
  178. void avfilter_end_frame(AVFilterLink *link)
  179. {
  180. void (*end_frame)(AVFilterLink *);
  181. if (!(end_frame = link->dstpad->end_frame))
  182. end_frame = avfilter_default_end_frame;
  183. end_frame(link);
  184. /* unreference the source picture if we're feeding the destination filter
  185. * a copied version dues to permission issues */
  186. if (link->src_buf) {
  187. avfilter_unref_buffer(link->src_buf);
  188. link->src_buf = NULL;
  189. }
  190. }
  191. void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  192. {
  193. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  194. }
  195. void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  196. {
  197. AVFilterLink *outlink = NULL;
  198. if (inlink->dst->output_count)
  199. outlink = inlink->dst->outputs[0];
  200. if (outlink)
  201. avfilter_draw_slice(outlink, y, h, slice_dir);
  202. }
  203. void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  204. {
  205. uint8_t *src[4], *dst[4];
  206. int i, j, vsub;
  207. void (*draw_slice)(AVFilterLink *, int, int, int);
  208. FF_DPRINTF_START(NULL, draw_slice); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
  209. /* copy the slice if needed for permission reasons */
  210. if (link->src_buf) {
  211. vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  212. for (i = 0; i < 4; i++) {
  213. if (link->src_buf->data[i]) {
  214. src[i] = link->src_buf-> data[i] +
  215. (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
  216. dst[i] = link->cur_buf->data[i] +
  217. (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
  218. } else
  219. src[i] = dst[i] = NULL;
  220. }
  221. for (i = 0; i < 4; i++) {
  222. int planew =
  223. av_image_get_linesize(link->format, link->cur_buf->video->w, i);
  224. if (!src[i]) continue;
  225. for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
  226. memcpy(dst[i], src[i], planew);
  227. src[i] += link->src_buf->linesize[i];
  228. dst[i] += link->cur_buf->linesize[i];
  229. }
  230. }
  231. }
  232. if (!(draw_slice = link->dstpad->draw_slice))
  233. draw_slice = avfilter_default_draw_slice;
  234. draw_slice(link, y, h, slice_dir);
  235. }