video.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * Copyright 2007 Bobby Bingham
  3. * Copyright Stefano Sabatini <stefasab gmail com>
  4. * Copyright Vitor Sessak <vitor1001 gmail com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/imgutils.h"
  23. #include "avfilter.h"
  24. #include "internal.h"
  25. #include "video.h"
  26. static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
  27. {
  28. snprintf(buf, buf_size, "%s%s%s%s%s%s",
  29. perms & AV_PERM_READ ? "r" : "",
  30. perms & AV_PERM_WRITE ? "w" : "",
  31. perms & AV_PERM_PRESERVE ? "p" : "",
  32. perms & AV_PERM_REUSE ? "u" : "",
  33. perms & AV_PERM_REUSE2 ? "U" : "",
  34. perms & AV_PERM_NEG_LINESIZES ? "n" : "");
  35. return buf;
  36. }
  37. static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
  38. {
  39. av_unused char buf[16];
  40. av_dlog(ctx,
  41. "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  42. ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
  43. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  44. ref->pts, ref->pos);
  45. if (ref->video) {
  46. av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
  47. ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
  48. ref->video->w, ref->video->h,
  49. !ref->video->interlaced ? 'P' : /* Progressive */
  50. ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
  51. ref->video->key_frame,
  52. av_get_picture_type_char(ref->video->pict_type));
  53. }
  54. if (ref->audio) {
  55. av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d",
  56. ref->audio->channel_layout,
  57. ref->audio->nb_samples,
  58. ref->audio->sample_rate);
  59. }
  60. av_dlog(ctx, "]%s", end ? "\n" : "");
  61. }
  62. AVFilterBufferRef *ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  63. {
  64. return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  65. }
  66. AVFilterBufferRef *ff_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  67. {
  68. int linesize[4];
  69. uint8_t *data[4];
  70. int i;
  71. AVFilterBufferRef *picref = NULL;
  72. AVFilterPool *pool = link->pool;
  73. if (pool) {
  74. for (i = 0; i < POOL_SIZE; i++) {
  75. picref = pool->pic[i];
  76. if (picref && picref->buf->format == link->format && picref->buf->w == w && picref->buf->h == h) {
  77. AVFilterBuffer *pic = picref->buf;
  78. pool->pic[i] = NULL;
  79. pool->count--;
  80. picref->video->w = w;
  81. picref->video->h = h;
  82. picref->perms = perms | AV_PERM_READ;
  83. picref->format = link->format;
  84. pic->refcount = 1;
  85. memcpy(picref->data, pic->data, sizeof(picref->data));
  86. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  87. pool->refcount++;
  88. return picref;
  89. }
  90. }
  91. } else {
  92. pool = link->pool = av_mallocz(sizeof(AVFilterPool));
  93. pool->refcount = 1;
  94. }
  95. // align: +2 is needed for swscaler, +16 to be SIMD-friendly
  96. if ((i = av_image_alloc(data, linesize, w, h, link->format, 32)) < 0)
  97. return NULL;
  98. picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
  99. perms, w, h, link->format);
  100. if (!picref) {
  101. av_free(data[0]);
  102. return NULL;
  103. }
  104. memset(data[0], 128, i);
  105. picref->buf->priv = pool;
  106. picref->buf->free = NULL;
  107. pool->refcount++;
  108. return picref;
  109. }
  110. AVFilterBufferRef *
  111. avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
  112. int w, int h, enum PixelFormat format)
  113. {
  114. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  115. AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
  116. if (!pic || !picref)
  117. goto fail;
  118. picref->buf = pic;
  119. picref->buf->free = ff_avfilter_default_free_buffer;
  120. if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
  121. goto fail;
  122. pic->w = picref->video->w = w;
  123. pic->h = picref->video->h = h;
  124. /* make sure the buffer gets read permission or it's useless for output */
  125. picref->perms = perms | AV_PERM_READ;
  126. pic->refcount = 1;
  127. picref->type = AVMEDIA_TYPE_VIDEO;
  128. pic->format = picref->format = format;
  129. memcpy(pic->data, data, 4*sizeof(data[0]));
  130. memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
  131. memcpy(picref->data, pic->data, sizeof(picref->data));
  132. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  133. pic-> extended_data = pic->data;
  134. picref->extended_data = picref->data;
  135. picref->pts = AV_NOPTS_VALUE;
  136. return picref;
  137. fail:
  138. if (picref && picref->video)
  139. av_free(picref->video);
  140. av_free(picref);
  141. av_free(pic);
  142. return NULL;
  143. }
  144. AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  145. {
  146. AVFilterBufferRef *ret = NULL;
  147. av_unused char buf[16];
  148. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
  149. av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
  150. if (link->dstpad->get_video_buffer)
  151. ret = link->dstpad->get_video_buffer(link, perms, w, h);
  152. if (!ret)
  153. ret = ff_default_get_video_buffer(link, perms, w, h);
  154. if (ret)
  155. ret->type = AVMEDIA_TYPE_VIDEO;
  156. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
  157. return ret;
  158. }
  159. void ff_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  160. {
  161. avfilter_start_frame(link->dst->outputs[0], picref);
  162. }
  163. static void default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  164. {
  165. AVFilterLink *outlink = NULL;
  166. if (inlink->dst->output_count)
  167. outlink = inlink->dst->outputs[0];
  168. if (outlink) {
  169. outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  170. avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
  171. avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
  172. }
  173. }
  174. /* XXX: should we do the duplicating of the picture ref here, instead of
  175. * forcing the source filter to do it? */
  176. void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  177. {
  178. void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
  179. AVFilterPad *dst = link->dstpad;
  180. int perms = picref->perms;
  181. AVFilterCommand *cmd= link->dst->command_queue;
  182. FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
  183. if (!(start_frame = dst->start_frame))
  184. start_frame = default_start_frame;
  185. if (picref->linesize[0] < 0)
  186. perms |= AV_PERM_NEG_LINESIZES;
  187. /* prepare to copy the picture if it has insufficient permissions */
  188. if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
  189. av_log(link->dst, AV_LOG_DEBUG,
  190. "frame copy needed (have perms %x, need %x, reject %x)\n",
  191. picref->perms,
  192. link->dstpad->min_perms, link->dstpad->rej_perms);
  193. link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
  194. link->src_buf = picref;
  195. avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
  196. /* copy palette if required */
  197. if (av_pix_fmt_descriptors[link->format].flags & PIX_FMT_PAL)
  198. memcpy(link->cur_buf->data[1], link->src_buf-> data[1], AVPALETTE_SIZE);
  199. }
  200. else
  201. link->cur_buf = picref;
  202. while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
  203. av_log(link->dst, AV_LOG_DEBUG,
  204. "Processing command time:%f command:%s arg:%s\n",
  205. cmd->time, cmd->command, cmd->arg);
  206. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  207. ff_command_queue_pop(link->dst);
  208. cmd= link->dst->command_queue;
  209. }
  210. start_frame(link, link->cur_buf);
  211. ff_update_link_current_pts(link, link->cur_buf->pts);
  212. }
  213. void ff_null_end_frame(AVFilterLink *link)
  214. {
  215. avfilter_end_frame(link->dst->outputs[0]);
  216. }
  217. static void default_end_frame(AVFilterLink *inlink)
  218. {
  219. AVFilterLink *outlink = NULL;
  220. if (inlink->dst->output_count)
  221. outlink = inlink->dst->outputs[0];
  222. avfilter_unref_buffer(inlink->cur_buf);
  223. inlink->cur_buf = NULL;
  224. if (outlink) {
  225. if (outlink->out_buf) {
  226. avfilter_unref_buffer(outlink->out_buf);
  227. outlink->out_buf = NULL;
  228. }
  229. avfilter_end_frame(outlink);
  230. }
  231. }
  232. void avfilter_end_frame(AVFilterLink *link)
  233. {
  234. void (*end_frame)(AVFilterLink *);
  235. if (!(end_frame = link->dstpad->end_frame))
  236. end_frame = default_end_frame;
  237. end_frame(link);
  238. /* unreference the source picture if we're feeding the destination filter
  239. * a copied version dues to permission issues */
  240. if (link->src_buf) {
  241. avfilter_unref_buffer(link->src_buf);
  242. link->src_buf = NULL;
  243. }
  244. }
  245. void ff_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  246. {
  247. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  248. }
  249. static void default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  250. {
  251. AVFilterLink *outlink = NULL;
  252. if (inlink->dst->output_count)
  253. outlink = inlink->dst->outputs[0];
  254. if (outlink)
  255. avfilter_draw_slice(outlink, y, h, slice_dir);
  256. }
  257. void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  258. {
  259. uint8_t *src[4], *dst[4];
  260. int i, j, vsub;
  261. void (*draw_slice)(AVFilterLink *, int, int, int);
  262. 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);
  263. /* copy the slice if needed for permission reasons */
  264. if (link->src_buf) {
  265. vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  266. for (i = 0; i < 4; i++) {
  267. if (link->src_buf->data[i]) {
  268. src[i] = link->src_buf-> data[i] +
  269. (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
  270. dst[i] = link->cur_buf->data[i] +
  271. (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
  272. } else
  273. src[i] = dst[i] = NULL;
  274. }
  275. for (i = 0; i < 4; i++) {
  276. int planew =
  277. av_image_get_linesize(link->format, link->cur_buf->video->w, i);
  278. if (!src[i]) continue;
  279. for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
  280. memcpy(dst[i], src[i], planew);
  281. src[i] += link->src_buf->linesize[i];
  282. dst[i] += link->cur_buf->linesize[i];
  283. }
  284. }
  285. }
  286. if (!(draw_slice = link->dstpad->draw_slice))
  287. draw_slice = default_draw_slice;
  288. draw_slice(link, y, h, slice_dir);
  289. }
  290. #if FF_API_FILTERS_PUBLIC
  291. AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  292. {
  293. return ff_default_get_video_buffer(link, perms, w, h);
  294. }
  295. void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  296. {
  297. default_start_frame(inlink, picref);
  298. }
  299. void avfilter_default_end_frame(AVFilterLink *inlink)
  300. {
  301. default_end_frame(inlink);
  302. }
  303. void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  304. {
  305. default_draw_slice(inlink, y, h, slice_dir);
  306. }
  307. AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  308. {
  309. return ff_null_get_video_buffer(link, perms, w, h);
  310. }
  311. void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  312. {
  313. ff_null_start_frame(link, picref);
  314. }
  315. void avfilter_null_end_frame(AVFilterLink *link)
  316. {
  317. ff_null_end_frame(link);
  318. }
  319. void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  320. {
  321. ff_null_draw_slice(link, y, h, slice_dir);
  322. }
  323. #endif