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.

422 lines
15KB

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