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.

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