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.

410 lines
14KB

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