vf_transpose.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright (c) 2010 Stefano Sabatini
  3. * Copyright (c) 2008 Vitor Sessak
  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. /**
  22. * @file
  23. * transposition filter
  24. * Based on MPlayer libmpcodecs/vf_rotate.c.
  25. */
  26. #include <stdio.h>
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/internal.h"
  30. #include "libavutil/intreadwrite.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "avfilter.h"
  34. #include "formats.h"
  35. #include "internal.h"
  36. #include "video.h"
  37. #include "transpose.h"
  38. typedef struct TransVtable {
  39. void (*transpose_8x8)(uint8_t *src, ptrdiff_t src_linesize,
  40. uint8_t *dst, ptrdiff_t dst_linesize);
  41. void (*transpose_block)(uint8_t *src, ptrdiff_t src_linesize,
  42. uint8_t *dst, ptrdiff_t dst_linesize,
  43. int w, int h);
  44. } TransVtable;
  45. typedef struct TransContext {
  46. const AVClass *class;
  47. int hsub, vsub;
  48. int planes;
  49. int pixsteps[4];
  50. int passthrough; ///< PassthroughType, landscape passthrough mode enabled
  51. int dir; ///< TransposeDir
  52. TransVtable vtables[4];
  53. } TransContext;
  54. static int query_formats(AVFilterContext *ctx)
  55. {
  56. AVFilterFormats *pix_fmts = NULL;
  57. int fmt, ret;
  58. for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
  59. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  60. if (!(desc->flags & AV_PIX_FMT_FLAG_PAL ||
  61. desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
  62. desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ||
  63. desc->log2_chroma_w != desc->log2_chroma_h) &&
  64. (ret = ff_add_format(&pix_fmts, fmt)) < 0)
  65. return ret;
  66. }
  67. return ff_set_common_formats(ctx, pix_fmts);
  68. }
  69. static inline void transpose_block_8_c(uint8_t *src, ptrdiff_t src_linesize,
  70. uint8_t *dst, ptrdiff_t dst_linesize,
  71. int w, int h)
  72. {
  73. int x, y;
  74. for (y = 0; y < h; y++, dst += dst_linesize, src++)
  75. for (x = 0; x < w; x++)
  76. dst[x] = src[x*src_linesize];
  77. }
  78. static void transpose_8x8_8_c(uint8_t *src, ptrdiff_t src_linesize,
  79. uint8_t *dst, ptrdiff_t dst_linesize)
  80. {
  81. transpose_block_8_c(src, src_linesize, dst, dst_linesize, 8, 8);
  82. }
  83. static inline void transpose_block_16_c(uint8_t *src, ptrdiff_t src_linesize,
  84. uint8_t *dst, ptrdiff_t dst_linesize,
  85. int w, int h)
  86. {
  87. int x, y;
  88. for (y = 0; y < h; y++, dst += dst_linesize, src += 2)
  89. for (x = 0; x < w; x++)
  90. *((uint16_t *)(dst + 2*x)) = *((uint16_t *)(src + x*src_linesize));
  91. }
  92. static void transpose_8x8_16_c(uint8_t *src, ptrdiff_t src_linesize,
  93. uint8_t *dst, ptrdiff_t dst_linesize)
  94. {
  95. transpose_block_16_c(src, src_linesize, dst, dst_linesize, 8, 8);
  96. }
  97. static inline void transpose_block_24_c(uint8_t *src, ptrdiff_t src_linesize,
  98. uint8_t *dst, ptrdiff_t dst_linesize,
  99. int w, int h)
  100. {
  101. int x, y;
  102. for (y = 0; y < h; y++, dst += dst_linesize) {
  103. for (x = 0; x < w; x++) {
  104. int32_t v = AV_RB24(src + x*src_linesize + y*3);
  105. AV_WB24(dst + 3*x, v);
  106. }
  107. }
  108. }
  109. static void transpose_8x8_24_c(uint8_t *src, ptrdiff_t src_linesize,
  110. uint8_t *dst, ptrdiff_t dst_linesize)
  111. {
  112. transpose_block_24_c(src, src_linesize, dst, dst_linesize, 8, 8);
  113. }
  114. static inline void transpose_block_32_c(uint8_t *src, ptrdiff_t src_linesize,
  115. uint8_t *dst, ptrdiff_t dst_linesize,
  116. int w, int h)
  117. {
  118. int x, y;
  119. for (y = 0; y < h; y++, dst += dst_linesize, src += 4) {
  120. for (x = 0; x < w; x++)
  121. *((uint32_t *)(dst + 4*x)) = *((uint32_t *)(src + x*src_linesize));
  122. }
  123. }
  124. static void transpose_8x8_32_c(uint8_t *src, ptrdiff_t src_linesize,
  125. uint8_t *dst, ptrdiff_t dst_linesize)
  126. {
  127. transpose_block_32_c(src, src_linesize, dst, dst_linesize, 8, 8);
  128. }
  129. static inline void transpose_block_48_c(uint8_t *src, ptrdiff_t src_linesize,
  130. uint8_t *dst, ptrdiff_t dst_linesize,
  131. int w, int h)
  132. {
  133. int x, y;
  134. for (y = 0; y < h; y++, dst += dst_linesize, src += 6) {
  135. for (x = 0; x < w; x++) {
  136. int64_t v = AV_RB48(src + x*src_linesize);
  137. AV_WB48(dst + 6*x, v);
  138. }
  139. }
  140. }
  141. static void transpose_8x8_48_c(uint8_t *src, ptrdiff_t src_linesize,
  142. uint8_t *dst, ptrdiff_t dst_linesize)
  143. {
  144. transpose_block_48_c(src, src_linesize, dst, dst_linesize, 8, 8);
  145. }
  146. static inline void transpose_block_64_c(uint8_t *src, ptrdiff_t src_linesize,
  147. uint8_t *dst, ptrdiff_t dst_linesize,
  148. int w, int h)
  149. {
  150. int x, y;
  151. for (y = 0; y < h; y++, dst += dst_linesize, src += 8)
  152. for (x = 0; x < w; x++)
  153. *((uint64_t *)(dst + 8*x)) = *((uint64_t *)(src + x*src_linesize));
  154. }
  155. static void transpose_8x8_64_c(uint8_t *src, ptrdiff_t src_linesize,
  156. uint8_t *dst, ptrdiff_t dst_linesize)
  157. {
  158. transpose_block_64_c(src, src_linesize, dst, dst_linesize, 8, 8);
  159. }
  160. static int config_props_output(AVFilterLink *outlink)
  161. {
  162. AVFilterContext *ctx = outlink->src;
  163. TransContext *s = ctx->priv;
  164. AVFilterLink *inlink = ctx->inputs[0];
  165. const AVPixFmtDescriptor *desc_out = av_pix_fmt_desc_get(outlink->format);
  166. const AVPixFmtDescriptor *desc_in = av_pix_fmt_desc_get(inlink->format);
  167. if (s->dir&4) {
  168. av_log(ctx, AV_LOG_WARNING,
  169. "dir values greater than 3 are deprecated, use the passthrough option instead\n");
  170. s->dir &= 3;
  171. s->passthrough = TRANSPOSE_PT_TYPE_LANDSCAPE;
  172. }
  173. if ((inlink->w >= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
  174. (inlink->w <= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
  175. av_log(ctx, AV_LOG_VERBOSE,
  176. "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
  177. inlink->w, inlink->h, inlink->w, inlink->h);
  178. return 0;
  179. } else {
  180. s->passthrough = TRANSPOSE_PT_TYPE_NONE;
  181. }
  182. s->hsub = desc_in->log2_chroma_w;
  183. s->vsub = desc_in->log2_chroma_h;
  184. s->planes = av_pix_fmt_count_planes(outlink->format);
  185. av_assert0(desc_in->nb_components == desc_out->nb_components);
  186. av_image_fill_max_pixsteps(s->pixsteps, NULL, desc_out);
  187. outlink->w = inlink->h;
  188. outlink->h = inlink->w;
  189. if (inlink->sample_aspect_ratio.num)
  190. outlink->sample_aspect_ratio = av_div_q((AVRational) { 1, 1 },
  191. inlink->sample_aspect_ratio);
  192. else
  193. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  194. for (int i = 0; i < 4; i++) {
  195. TransVtable *v = &s->vtables[i];
  196. switch (s->pixsteps[i]) {
  197. case 1: v->transpose_block = transpose_block_8_c;
  198. v->transpose_8x8 = transpose_8x8_8_c; break;
  199. case 2: v->transpose_block = transpose_block_16_c;
  200. v->transpose_8x8 = transpose_8x8_16_c; break;
  201. case 3: v->transpose_block = transpose_block_24_c;
  202. v->transpose_8x8 = transpose_8x8_24_c; break;
  203. case 4: v->transpose_block = transpose_block_32_c;
  204. v->transpose_8x8 = transpose_8x8_32_c; break;
  205. case 6: v->transpose_block = transpose_block_48_c;
  206. v->transpose_8x8 = transpose_8x8_48_c; break;
  207. case 8: v->transpose_block = transpose_block_64_c;
  208. v->transpose_8x8 = transpose_8x8_64_c; break;
  209. }
  210. }
  211. av_log(ctx, AV_LOG_VERBOSE,
  212. "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
  213. inlink->w, inlink->h, s->dir, outlink->w, outlink->h,
  214. s->dir == 1 || s->dir == 3 ? "clockwise" : "counterclockwise",
  215. s->dir == 0 || s->dir == 3);
  216. return 0;
  217. }
  218. static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
  219. {
  220. TransContext *s = inlink->dst->priv;
  221. return s->passthrough ?
  222. ff_null_get_video_buffer (inlink, w, h) :
  223. ff_default_get_video_buffer(inlink, w, h);
  224. }
  225. typedef struct ThreadData {
  226. AVFrame *in, *out;
  227. } ThreadData;
  228. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr,
  229. int nb_jobs)
  230. {
  231. TransContext *s = ctx->priv;
  232. ThreadData *td = arg;
  233. AVFrame *out = td->out;
  234. AVFrame *in = td->in;
  235. int plane;
  236. for (plane = 0; plane < s->planes; plane++) {
  237. int hsub = plane == 1 || plane == 2 ? s->hsub : 0;
  238. int vsub = plane == 1 || plane == 2 ? s->vsub : 0;
  239. int pixstep = s->pixsteps[plane];
  240. int inh = AV_CEIL_RSHIFT(in->height, vsub);
  241. int outw = AV_CEIL_RSHIFT(out->width, hsub);
  242. int outh = AV_CEIL_RSHIFT(out->height, vsub);
  243. int start = (outh * jobnr ) / nb_jobs;
  244. int end = (outh * (jobnr+1)) / nb_jobs;
  245. uint8_t *dst, *src;
  246. int dstlinesize, srclinesize;
  247. int x, y;
  248. TransVtable *v = &s->vtables[plane];
  249. dstlinesize = out->linesize[plane];
  250. dst = out->data[plane] + start * dstlinesize;
  251. src = in->data[plane];
  252. srclinesize = in->linesize[plane];
  253. if (s->dir & 1) {
  254. src += in->linesize[plane] * (inh - 1);
  255. srclinesize *= -1;
  256. }
  257. if (s->dir & 2) {
  258. dst = out->data[plane] + dstlinesize * (outh - start - 1);
  259. dstlinesize *= -1;
  260. }
  261. for (y = start; y < end - 7; y += 8) {
  262. for (x = 0; x < outw - 7; x += 8) {
  263. v->transpose_8x8(src + x * srclinesize + y * pixstep,
  264. srclinesize,
  265. dst + (y - start) * dstlinesize + x * pixstep,
  266. dstlinesize);
  267. }
  268. if (outw - x > 0 && end - y > 0)
  269. v->transpose_block(src + x * srclinesize + y * pixstep,
  270. srclinesize,
  271. dst + (y - start) * dstlinesize + x * pixstep,
  272. dstlinesize, outw - x, end - y);
  273. }
  274. if (end - y > 0)
  275. v->transpose_block(src + 0 * srclinesize + y * pixstep,
  276. srclinesize,
  277. dst + (y - start) * dstlinesize + 0 * pixstep,
  278. dstlinesize, outw, end - y);
  279. }
  280. return 0;
  281. }
  282. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  283. {
  284. AVFilterContext *ctx = inlink->dst;
  285. TransContext *s = ctx->priv;
  286. AVFilterLink *outlink = ctx->outputs[0];
  287. ThreadData td;
  288. AVFrame *out;
  289. if (s->passthrough)
  290. return ff_filter_frame(outlink, in);
  291. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  292. if (!out) {
  293. av_frame_free(&in);
  294. return AVERROR(ENOMEM);
  295. }
  296. av_frame_copy_props(out, in);
  297. if (in->sample_aspect_ratio.num == 0) {
  298. out->sample_aspect_ratio = in->sample_aspect_ratio;
  299. } else {
  300. out->sample_aspect_ratio.num = in->sample_aspect_ratio.den;
  301. out->sample_aspect_ratio.den = in->sample_aspect_ratio.num;
  302. }
  303. td.in = in, td.out = out;
  304. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  305. av_frame_free(&in);
  306. return ff_filter_frame(outlink, out);
  307. }
  308. #define OFFSET(x) offsetof(TransContext, x)
  309. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  310. static const AVOption transpose_options[] = {
  311. { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP }, 0, 7, FLAGS, "dir" },
  312. { "cclock_flip", "rotate counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .flags=FLAGS, .unit = "dir" },
  313. { "clock", "rotate clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .flags=FLAGS, .unit = "dir" },
  314. { "cclock", "rotate counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .flags=FLAGS, .unit = "dir" },
  315. { "clock_flip", "rotate clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .flags=FLAGS, .unit = "dir" },
  316. { "passthrough", "do not apply transposition if the input matches the specified geometry",
  317. OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE}, 0, INT_MAX, FLAGS, "passthrough" },
  318. { "none", "always apply transposition", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
  319. { "portrait", "preserve portrait geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
  320. { "landscape", "preserve landscape geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
  321. { NULL }
  322. };
  323. AVFILTER_DEFINE_CLASS(transpose);
  324. static const AVFilterPad avfilter_vf_transpose_inputs[] = {
  325. {
  326. .name = "default",
  327. .type = AVMEDIA_TYPE_VIDEO,
  328. .get_video_buffer = get_video_buffer,
  329. .filter_frame = filter_frame,
  330. },
  331. { NULL }
  332. };
  333. static const AVFilterPad avfilter_vf_transpose_outputs[] = {
  334. {
  335. .name = "default",
  336. .config_props = config_props_output,
  337. .type = AVMEDIA_TYPE_VIDEO,
  338. },
  339. { NULL }
  340. };
  341. AVFilter ff_vf_transpose = {
  342. .name = "transpose",
  343. .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
  344. .priv_size = sizeof(TransContext),
  345. .priv_class = &transpose_class,
  346. .query_formats = query_formats,
  347. .inputs = avfilter_vf_transpose_inputs,
  348. .outputs = avfilter_vf_transpose_outputs,
  349. .flags = AVFILTER_FLAG_SLICE_THREADS,
  350. };