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.

347 lines
11KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
  22. {
  23. snprintf(buf, buf_size, "%s%s%s%s%s%s",
  24. perms & AV_PERM_READ ? "r" : "",
  25. perms & AV_PERM_WRITE ? "w" : "",
  26. perms & AV_PERM_PRESERVE ? "p" : "",
  27. perms & AV_PERM_REUSE ? "u" : "",
  28. perms & AV_PERM_REUSE2 ? "U" : "",
  29. perms & AV_PERM_NEG_LINESIZES ? "n" : "");
  30. return buf;
  31. }
  32. static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
  33. {
  34. av_unused char buf[16];
  35. av_dlog(ctx,
  36. "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  37. ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
  38. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  39. ref->pts, ref->pos);
  40. if (ref->video) {
  41. av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
  42. ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
  43. ref->video->w, ref->video->h,
  44. !ref->video->interlaced ? 'P' : /* Progressive */
  45. ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
  46. ref->video->key_frame,
  47. av_get_picture_type_char(ref->video->pict_type));
  48. }
  49. if (ref->audio) {
  50. av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d",
  51. ref->audio->channel_layout,
  52. ref->audio->nb_samples,
  53. ref->audio->sample_rate);
  54. }
  55. av_dlog(ctx, "]%s", end ? "\n" : "");
  56. }
  57. AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  58. {
  59. return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  60. }
  61. AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  62. {
  63. int linesize[4];
  64. uint8_t *data[4];
  65. int i;
  66. AVFilterBufferRef *picref = NULL;
  67. AVFilterPool *pool = link->pool;
  68. if (pool) {
  69. for (i = 0; i < POOL_SIZE; i++) {
  70. picref = pool->pic[i];
  71. if (picref && picref->buf->format == link->format && picref->buf->w == w && picref->buf->h == h) {
  72. AVFilterBuffer *pic = picref->buf;
  73. pool->pic[i] = NULL;
  74. pool->count--;
  75. picref->video->w = w;
  76. picref->video->h = h;
  77. picref->perms = perms | AV_PERM_READ;
  78. picref->format = link->format;
  79. pic->refcount = 1;
  80. memcpy(picref->data, pic->data, sizeof(picref->data));
  81. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  82. pool->refcount++;
  83. return picref;
  84. }
  85. }
  86. } else {
  87. pool = link->pool = av_mallocz(sizeof(AVFilterPool));
  88. pool->refcount = 1;
  89. }
  90. // align: +2 is needed for swscaler, +16 to be SIMD-friendly
  91. if ((i = av_image_alloc(data, linesize, w, h, link->format, 32)) < 0)
  92. return NULL;
  93. picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
  94. perms, w, h, link->format);
  95. if (!picref) {
  96. av_free(data[0]);
  97. return NULL;
  98. }
  99. memset(data[0], 128, i);
  100. picref->buf->priv = pool;
  101. picref->buf->free = NULL;
  102. pool->refcount++;
  103. return picref;
  104. }
  105. AVFilterBufferRef *
  106. avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
  107. int w, int h, enum PixelFormat format)
  108. {
  109. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  110. AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
  111. if (!pic || !picref)
  112. goto fail;
  113. picref->buf = pic;
  114. picref->buf->free = ff_avfilter_default_free_buffer;
  115. if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
  116. goto fail;
  117. pic->w = picref->video->w = w;
  118. pic->h = picref->video->h = h;
  119. /* make sure the buffer gets read permission or it's useless for output */
  120. picref->perms = perms | AV_PERM_READ;
  121. pic->refcount = 1;
  122. picref->type = AVMEDIA_TYPE_VIDEO;
  123. pic->format = picref->format = format;
  124. memcpy(pic->data, data, 4*sizeof(data[0]));
  125. memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
  126. memcpy(picref->data, pic->data, sizeof(picref->data));
  127. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  128. pic-> extended_data = pic->data;
  129. picref->extended_data = picref->data;
  130. picref->pts = AV_NOPTS_VALUE;
  131. return picref;
  132. fail:
  133. if (picref && picref->video)
  134. av_free(picref->video);
  135. av_free(picref);
  136. av_free(pic);
  137. return NULL;
  138. }
  139. AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  140. {
  141. AVFilterBufferRef *ret = NULL;
  142. av_unused char buf[16];
  143. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
  144. av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
  145. if (link->dstpad->get_video_buffer)
  146. ret = link->dstpad->get_video_buffer(link, perms, w, h);
  147. if (!ret)
  148. ret = avfilter_default_get_video_buffer(link, perms, w, h);
  149. if (ret)
  150. ret->type = AVMEDIA_TYPE_VIDEO;
  151. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
  152. return ret;
  153. }
  154. void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  155. {
  156. avfilter_start_frame(link->dst->outputs[0], picref);
  157. }
  158. void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  159. {
  160. AVFilterLink *outlink = NULL;
  161. if (inlink->dst->output_count)
  162. outlink = inlink->dst->outputs[0];
  163. if (outlink) {
  164. outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  165. avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
  166. avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
  167. }
  168. }
  169. /* XXX: should we do the duplicating of the picture ref here, instead of
  170. * forcing the source filter to do it? */
  171. void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  172. {
  173. void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
  174. AVFilterPad *dst = link->dstpad;
  175. int perms = picref->perms;
  176. AVFilterCommand *cmd= link->dst->command_queue;
  177. FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
  178. if (!(start_frame = dst->start_frame))
  179. start_frame = avfilter_default_start_frame;
  180. if (picref->linesize[0] < 0)
  181. perms |= AV_PERM_NEG_LINESIZES;
  182. /* prepare to copy the picture if it has insufficient permissions */
  183. if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
  184. av_log(link->dst, AV_LOG_DEBUG,
  185. "frame copy needed (have perms %x, need %x, reject %x)\n",
  186. picref->perms,
  187. link->dstpad->min_perms, link->dstpad->rej_perms);
  188. link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
  189. link->src_buf = picref;
  190. avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
  191. }
  192. else
  193. link->cur_buf = picref;
  194. while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
  195. av_log(link->dst, AV_LOG_DEBUG,
  196. "Processing command time:%f command:%s arg:%s\n",
  197. cmd->time, cmd->command, cmd->arg);
  198. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  199. ff_command_queue_pop(link->dst);
  200. cmd= link->dst->command_queue;
  201. }
  202. start_frame(link, link->cur_buf);
  203. ff_update_link_current_pts(link, link->cur_buf->pts);
  204. }
  205. void avfilter_null_end_frame(AVFilterLink *link)
  206. {
  207. avfilter_end_frame(link->dst->outputs[0]);
  208. }
  209. void avfilter_default_end_frame(AVFilterLink *inlink)
  210. {
  211. AVFilterLink *outlink = NULL;
  212. if (inlink->dst->output_count)
  213. outlink = inlink->dst->outputs[0];
  214. avfilter_unref_buffer(inlink->cur_buf);
  215. inlink->cur_buf = NULL;
  216. if (outlink) {
  217. if (outlink->out_buf) {
  218. avfilter_unref_buffer(outlink->out_buf);
  219. outlink->out_buf = NULL;
  220. }
  221. avfilter_end_frame(outlink);
  222. }
  223. }
  224. void avfilter_end_frame(AVFilterLink *link)
  225. {
  226. void (*end_frame)(AVFilterLink *);
  227. if (!(end_frame = link->dstpad->end_frame))
  228. end_frame = avfilter_default_end_frame;
  229. end_frame(link);
  230. /* unreference the source picture if we're feeding the destination filter
  231. * a copied version dues to permission issues */
  232. if (link->src_buf) {
  233. avfilter_unref_buffer(link->src_buf);
  234. link->src_buf = NULL;
  235. }
  236. }
  237. void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  238. {
  239. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  240. }
  241. void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  242. {
  243. AVFilterLink *outlink = NULL;
  244. if (inlink->dst->output_count)
  245. outlink = inlink->dst->outputs[0];
  246. if (outlink)
  247. avfilter_draw_slice(outlink, y, h, slice_dir);
  248. }
  249. void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  250. {
  251. uint8_t *src[4], *dst[4];
  252. int i, j, vsub;
  253. void (*draw_slice)(AVFilterLink *, int, int, int);
  254. 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);
  255. /* copy the slice if needed for permission reasons */
  256. if (link->src_buf) {
  257. vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  258. for (i = 0; i < 4; i++) {
  259. if (link->src_buf->data[i]) {
  260. src[i] = link->src_buf-> data[i] +
  261. (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
  262. dst[i] = link->cur_buf->data[i] +
  263. (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
  264. } else
  265. src[i] = dst[i] = NULL;
  266. }
  267. for (i = 0; i < 4; i++) {
  268. int planew =
  269. av_image_get_linesize(link->format, link->cur_buf->video->w, i);
  270. if (!src[i]) continue;
  271. for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
  272. memcpy(dst[i], src[i], planew);
  273. src[i] += link->src_buf->linesize[i];
  274. dst[i] += link->cur_buf->linesize[i];
  275. }
  276. }
  277. }
  278. if (!(draw_slice = link->dstpad->draw_slice))
  279. draw_slice = avfilter_default_draw_slice;
  280. draw_slice(link, y, h, slice_dir);
  281. }