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.

408 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/imgutils.h"
  28. #include "libavutil/internal.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "avfilter.h"
  33. #include "formats.h"
  34. #include "internal.h"
  35. #include "video.h"
  36. typedef enum {
  37. TRANSPOSE_PT_TYPE_NONE,
  38. TRANSPOSE_PT_TYPE_LANDSCAPE,
  39. TRANSPOSE_PT_TYPE_PORTRAIT,
  40. } PassthroughType;
  41. enum TransposeDir {
  42. TRANSPOSE_CCLOCK_FLIP,
  43. TRANSPOSE_CLOCK,
  44. TRANSPOSE_CCLOCK,
  45. TRANSPOSE_CLOCK_FLIP,
  46. };
  47. typedef struct TransContext {
  48. const AVClass *class;
  49. int hsub, vsub;
  50. int pixsteps[4];
  51. int passthrough; ///< PassthroughType, landscape passthrough mode enabled
  52. int dir; ///< TransposeDir
  53. void (*transpose_8x8)(uint8_t *src, ptrdiff_t src_linesize,
  54. uint8_t *dst, ptrdiff_t dst_linesize);
  55. void (*transpose_block)(uint8_t *src, ptrdiff_t src_linesize,
  56. uint8_t *dst, ptrdiff_t dst_linesize,
  57. int w, int h);
  58. } TransContext;
  59. static int query_formats(AVFilterContext *ctx)
  60. {
  61. AVFilterFormats *pix_fmts = NULL;
  62. int fmt, ret;
  63. for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
  64. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  65. if (!(desc->flags & AV_PIX_FMT_FLAG_PAL ||
  66. desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
  67. desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ||
  68. desc->log2_chroma_w != desc->log2_chroma_h) &&
  69. (ret = ff_add_format(&pix_fmts, fmt)) < 0)
  70. return ret;
  71. }
  72. return ff_set_common_formats(ctx, pix_fmts);
  73. }
  74. static inline void transpose_block_8_c(uint8_t *src, ptrdiff_t src_linesize,
  75. uint8_t *dst, ptrdiff_t dst_linesize,
  76. int w, int h)
  77. {
  78. int x, y;
  79. for (y = 0; y < h; y++, dst += dst_linesize, src++)
  80. for (x = 0; x < w; x++)
  81. dst[x] = src[x*src_linesize];
  82. }
  83. static void transpose_8x8_8_c(uint8_t *src, ptrdiff_t src_linesize,
  84. uint8_t *dst, ptrdiff_t dst_linesize)
  85. {
  86. transpose_block_8_c(src, src_linesize, dst, dst_linesize, 8, 8);
  87. }
  88. static inline void transpose_block_16_c(uint8_t *src, ptrdiff_t src_linesize,
  89. uint8_t *dst, ptrdiff_t dst_linesize,
  90. int w, int h)
  91. {
  92. int x, y;
  93. for (y = 0; y < h; y++, dst += dst_linesize, src += 2)
  94. for (x = 0; x < w; x++)
  95. *((uint16_t *)(dst + 2*x)) = *((uint16_t *)(src + x*src_linesize));
  96. }
  97. static void transpose_8x8_16_c(uint8_t *src, ptrdiff_t src_linesize,
  98. uint8_t *dst, ptrdiff_t dst_linesize)
  99. {
  100. transpose_block_16_c(src, src_linesize, dst, dst_linesize, 8, 8);
  101. }
  102. static inline void transpose_block_24_c(uint8_t *src, ptrdiff_t src_linesize,
  103. uint8_t *dst, ptrdiff_t dst_linesize,
  104. int w, int h)
  105. {
  106. int x, y;
  107. for (y = 0; y < h; y++, dst += dst_linesize) {
  108. for (x = 0; x < w; x++) {
  109. int32_t v = AV_RB24(src + x*src_linesize + y*3);
  110. AV_WB24(dst + 3*x, v);
  111. }
  112. }
  113. }
  114. static void transpose_8x8_24_c(uint8_t *src, ptrdiff_t src_linesize,
  115. uint8_t *dst, ptrdiff_t dst_linesize)
  116. {
  117. transpose_block_24_c(src, src_linesize, dst, dst_linesize, 8, 8);
  118. }
  119. static inline void transpose_block_32_c(uint8_t *src, ptrdiff_t src_linesize,
  120. uint8_t *dst, ptrdiff_t dst_linesize,
  121. int w, int h)
  122. {
  123. int x, y;
  124. for (y = 0; y < h; y++, dst += dst_linesize, src += 4) {
  125. for (x = 0; x < w; x++)
  126. *((uint32_t *)(dst + 4*x)) = *((uint32_t *)(src + x*src_linesize));
  127. }
  128. }
  129. static void transpose_8x8_32_c(uint8_t *src, ptrdiff_t src_linesize,
  130. uint8_t *dst, ptrdiff_t dst_linesize)
  131. {
  132. transpose_block_32_c(src, src_linesize, dst, dst_linesize, 8, 8);
  133. }
  134. static inline void transpose_block_48_c(uint8_t *src, ptrdiff_t src_linesize,
  135. uint8_t *dst, ptrdiff_t dst_linesize,
  136. int w, int h)
  137. {
  138. int x, y;
  139. for (y = 0; y < h; y++, dst += dst_linesize, src += 6) {
  140. for (x = 0; x < w; x++) {
  141. int64_t v = AV_RB48(src + x*src_linesize);
  142. AV_WB48(dst + 6*x, v);
  143. }
  144. }
  145. }
  146. static void transpose_8x8_48_c(uint8_t *src, ptrdiff_t src_linesize,
  147. uint8_t *dst, ptrdiff_t dst_linesize)
  148. {
  149. transpose_block_48_c(src, src_linesize, dst, dst_linesize, 8, 8);
  150. }
  151. static inline void transpose_block_64_c(uint8_t *src, ptrdiff_t src_linesize,
  152. uint8_t *dst, ptrdiff_t dst_linesize,
  153. int w, int h)
  154. {
  155. int x, y;
  156. for (y = 0; y < h; y++, dst += dst_linesize, src += 8)
  157. for (x = 0; x < w; x++)
  158. *((uint64_t *)(dst + 8*x)) = *((uint64_t *)(src + x*src_linesize));
  159. }
  160. static void transpose_8x8_64_c(uint8_t *src, ptrdiff_t src_linesize,
  161. uint8_t *dst, ptrdiff_t dst_linesize)
  162. {
  163. transpose_block_64_c(src, src_linesize, dst, dst_linesize, 8, 8);
  164. }
  165. static int config_props_output(AVFilterLink *outlink)
  166. {
  167. AVFilterContext *ctx = outlink->src;
  168. TransContext *s = ctx->priv;
  169. AVFilterLink *inlink = ctx->inputs[0];
  170. const AVPixFmtDescriptor *desc_out = av_pix_fmt_desc_get(outlink->format);
  171. const AVPixFmtDescriptor *desc_in = av_pix_fmt_desc_get(inlink->format);
  172. if (s->dir&4) {
  173. av_log(ctx, AV_LOG_WARNING,
  174. "dir values greater than 3 are deprecated, use the passthrough option instead\n");
  175. s->dir &= 3;
  176. s->passthrough = TRANSPOSE_PT_TYPE_LANDSCAPE;
  177. }
  178. if ((inlink->w >= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
  179. (inlink->w <= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
  180. av_log(ctx, AV_LOG_VERBOSE,
  181. "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
  182. inlink->w, inlink->h, inlink->w, inlink->h);
  183. return 0;
  184. } else {
  185. s->passthrough = TRANSPOSE_PT_TYPE_NONE;
  186. }
  187. s->hsub = desc_in->log2_chroma_w;
  188. s->vsub = desc_in->log2_chroma_h;
  189. av_image_fill_max_pixsteps(s->pixsteps, NULL, desc_out);
  190. outlink->w = inlink->h;
  191. outlink->h = inlink->w;
  192. if (inlink->sample_aspect_ratio.num)
  193. outlink->sample_aspect_ratio = av_div_q((AVRational) { 1, 1 },
  194. inlink->sample_aspect_ratio);
  195. else
  196. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  197. switch (s->pixsteps[0]) {
  198. case 1: s->transpose_block = transpose_block_8_c;
  199. s->transpose_8x8 = transpose_8x8_8_c; break;
  200. case 2: s->transpose_block = transpose_block_16_c;
  201. s->transpose_8x8 = transpose_8x8_16_c; break;
  202. case 3: s->transpose_block = transpose_block_24_c;
  203. s->transpose_8x8 = transpose_8x8_24_c; break;
  204. case 4: s->transpose_block = transpose_block_32_c;
  205. s->transpose_8x8 = transpose_8x8_32_c; break;
  206. case 6: s->transpose_block = transpose_block_48_c;
  207. s->transpose_8x8 = transpose_8x8_48_c; break;
  208. case 8: s->transpose_block = transpose_block_64_c;
  209. s->transpose_8x8 = transpose_8x8_64_c; break;
  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; out->data[plane]; 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. 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. s->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. s->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. s->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. };