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.

416 lines
13KB

  1. /*
  2. * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
  3. * Copyright (c) 2014 Arwa Arif <arwaarif1994@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (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
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. /**
  22. * @file
  23. * Postprocessing filter - 7
  24. *
  25. * Originally written by Michael Niedermayer for the MPlayer
  26. * project, and ported by Arwa Arif for FFmpeg.
  27. */
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/mem_internal.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "internal.h"
  34. #include "qp_table.h"
  35. #include "vf_pp7.h"
  36. enum mode {
  37. MODE_HARD,
  38. MODE_SOFT,
  39. MODE_MEDIUM
  40. };
  41. #define OFFSET(x) offsetof(PP7Context, x)
  42. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  43. static const AVOption pp7_options[] = {
  44. { "qp", "force a constant quantizer parameter", OFFSET(qp), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 64, FLAGS },
  45. { "mode", "set thresholding mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MODE_MEDIUM}, 0, 2, FLAGS, "mode" },
  46. { "hard", "hard thresholding", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_HARD}, INT_MIN, INT_MAX, FLAGS, "mode" },
  47. { "soft", "soft thresholding", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_SOFT}, INT_MIN, INT_MAX, FLAGS, "mode" },
  48. { "medium", "medium thresholding", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_MEDIUM}, INT_MIN, INT_MAX, FLAGS, "mode" },
  49. { NULL }
  50. };
  51. AVFILTER_DEFINE_CLASS(pp7);
  52. DECLARE_ALIGNED(8, static const uint8_t, dither)[8][8] = {
  53. { 0, 48, 12, 60, 3, 51, 15, 63, },
  54. { 32, 16, 44, 28, 35, 19, 47, 31, },
  55. { 8, 56, 4, 52, 11, 59, 7, 55, },
  56. { 40, 24, 36, 20, 43, 27, 39, 23, },
  57. { 2, 50, 14, 62, 1, 49, 13, 61, },
  58. { 34, 18, 46, 30, 33, 17, 45, 29, },
  59. { 10, 58, 6, 54, 9, 57, 5, 53, },
  60. { 42, 26, 38, 22, 41, 25, 37, 21, },
  61. };
  62. #define N0 4
  63. #define N1 5
  64. #define N2 10
  65. #define SN0 2
  66. #define SN1 2.2360679775
  67. #define SN2 3.16227766017
  68. #define N (1 << 16)
  69. static const int factor[16] = {
  70. N / (N0 * N0), N / (N0 * N1), N / (N0 * N0), N / (N0 * N2),
  71. N / (N1 * N0), N / (N1 * N1), N / (N1 * N0), N / (N1 * N2),
  72. N / (N0 * N0), N / (N0 * N1), N / (N0 * N0), N / (N0 * N2),
  73. N / (N2 * N0), N / (N2 * N1), N / (N2 * N0), N / (N2 * N2),
  74. };
  75. static void init_thres2(PP7Context *p)
  76. {
  77. int qp, i;
  78. int bias = 0; //FIXME
  79. for (qp = 0; qp < 99; qp++) {
  80. for (i = 0; i < 16; i++) {
  81. p->thres2[qp][i] = ((i&1) ? SN2 : SN0) * ((i&4) ? SN2 : SN0) * FFMAX(1, qp) * (1<<2) - 1 - bias;
  82. }
  83. }
  84. }
  85. static inline void dctA_c(int16_t *dst, uint8_t *src, int stride)
  86. {
  87. int i;
  88. for (i = 0; i < 4; i++) {
  89. int s0 = src[0 * stride] + src[6 * stride];
  90. int s1 = src[1 * stride] + src[5 * stride];
  91. int s2 = src[2 * stride] + src[4 * stride];
  92. int s3 = src[3 * stride];
  93. int s = s3 + s3;
  94. s3 = s - s0;
  95. s0 = s + s0;
  96. s = s2 + s1;
  97. s2 = s2 - s1;
  98. dst[0] = s0 + s;
  99. dst[2] = s0 - s;
  100. dst[1] = 2 * s3 + s2;
  101. dst[3] = s3 - 2 * s2;
  102. src++;
  103. dst += 4;
  104. }
  105. }
  106. static void dctB_c(int16_t *dst, int16_t *src)
  107. {
  108. int i;
  109. for (i = 0; i < 4; i++) {
  110. int s0 = src[0 * 4] + src[6 * 4];
  111. int s1 = src[1 * 4] + src[5 * 4];
  112. int s2 = src[2 * 4] + src[4 * 4];
  113. int s3 = src[3 * 4];
  114. int s = s3 + s3;
  115. s3 = s - s0;
  116. s0 = s + s0;
  117. s = s2 + s1;
  118. s2 = s2 - s1;
  119. dst[0 * 4] = s0 + s;
  120. dst[2 * 4] = s0 - s;
  121. dst[1 * 4] = 2 * s3 + s2;
  122. dst[3 * 4] = s3 - 2 * s2;
  123. src++;
  124. dst++;
  125. }
  126. }
  127. static int hardthresh_c(PP7Context *p, int16_t *src, int qp)
  128. {
  129. int i;
  130. int a;
  131. a = src[0] * factor[0];
  132. for (i = 1; i < 16; i++) {
  133. unsigned int threshold1 = p->thres2[qp][i];
  134. unsigned int threshold2 = threshold1 << 1;
  135. int level = src[i];
  136. if (((unsigned)(level + threshold1)) > threshold2)
  137. a += level * factor[i];
  138. }
  139. return (a + (1 << 11)) >> 12;
  140. }
  141. static int mediumthresh_c(PP7Context *p, int16_t *src, int qp)
  142. {
  143. int i;
  144. int a;
  145. a = src[0] * factor[0];
  146. for (i = 1; i < 16; i++) {
  147. unsigned int threshold1 = p->thres2[qp][i];
  148. unsigned int threshold2 = threshold1 << 1;
  149. int level = src[i];
  150. if (((unsigned)(level + threshold1)) > threshold2) {
  151. if (((unsigned)(level + 2 * threshold1)) > 2 * threshold2)
  152. a += level * factor[i];
  153. else {
  154. if (level > 0)
  155. a += 2 * (level - (int)threshold1) * factor[i];
  156. else
  157. a += 2 * (level + (int)threshold1) * factor[i];
  158. }
  159. }
  160. }
  161. return (a + (1 << 11)) >> 12;
  162. }
  163. static int softthresh_c(PP7Context *p, int16_t *src, int qp)
  164. {
  165. int i;
  166. int a;
  167. a = src[0] * factor[0];
  168. for (i = 1; i < 16; i++) {
  169. unsigned int threshold1 = p->thres2[qp][i];
  170. unsigned int threshold2 = threshold1 << 1;
  171. int level = src[i];
  172. if (((unsigned)(level + threshold1)) > threshold2) {
  173. if (level > 0)
  174. a += (level - (int)threshold1) * factor[i];
  175. else
  176. a += (level + (int)threshold1) * factor[i];
  177. }
  178. }
  179. return (a + (1 << 11)) >> 12;
  180. }
  181. static void filter(PP7Context *p, uint8_t *dst, uint8_t *src,
  182. int dst_stride, int src_stride,
  183. int width, int height,
  184. uint8_t *qp_store, int qp_stride, int is_luma)
  185. {
  186. int x, y;
  187. const int stride = is_luma ? p->temp_stride : ((width + 16 + 15) & (~15));
  188. uint8_t *p_src = p->src + 8 * stride;
  189. int16_t *block = (int16_t *)p->src;
  190. int16_t *temp = (int16_t *)(p->src + 32);
  191. if (!src || !dst) return;
  192. for (y = 0; y < height; y++) {
  193. int index = 8 + 8 * stride + y * stride;
  194. memcpy(p_src + index, src + y * src_stride, width);
  195. for (x = 0; x < 8; x++) {
  196. p_src[index - x - 1]= p_src[index + x ];
  197. p_src[index + width + x ]= p_src[index + width - x - 1];
  198. }
  199. }
  200. for (y = 0; y < 8; y++) {
  201. memcpy(p_src + ( 7 - y ) * stride, p_src + ( y + 8 ) * stride, stride);
  202. memcpy(p_src + (height + 8 + y) * stride, p_src + (height - y + 7) * stride, stride);
  203. }
  204. //FIXME (try edge emu)
  205. for (y = 0; y < height; y++) {
  206. for (x = -8; x < 0; x += 4) {
  207. const int index = x + y * stride + (8 - 3) * (1 + stride) + 8; //FIXME silly offset
  208. uint8_t *src = p_src + index;
  209. int16_t *tp = temp + 4 * x;
  210. dctA_c(tp + 4 * 8, src, stride);
  211. }
  212. for (x = 0; x < width; ) {
  213. const int qps = 3 + is_luma;
  214. int qp;
  215. int end = FFMIN(x + 8, width);
  216. if (p->qp)
  217. qp = p->qp;
  218. else {
  219. qp = qp_store[ (FFMIN(x, width - 1) >> qps) + (FFMIN(y, height - 1) >> qps) * qp_stride];
  220. qp = ff_norm_qscale(qp, p->qscale_type);
  221. }
  222. for (; x < end; x++) {
  223. const int index = x + y * stride + (8 - 3) * (1 + stride) + 8; //FIXME silly offset
  224. uint8_t *src = p_src + index;
  225. int16_t *tp = temp + 4 * x;
  226. int v;
  227. if ((x & 3) == 0)
  228. dctA_c(tp + 4 * 8, src, stride);
  229. p->dctB(block, tp);
  230. v = p->requantize(p, block, qp);
  231. v = (v + dither[y & 7][x & 7]) >> 6;
  232. if ((unsigned)v > 255)
  233. v = (-v) >> 31;
  234. dst[x + y * dst_stride] = v;
  235. }
  236. }
  237. }
  238. }
  239. static int query_formats(AVFilterContext *ctx)
  240. {
  241. static const enum AVPixelFormat pix_fmts[] = {
  242. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  243. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  244. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  245. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
  246. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
  247. AV_PIX_FMT_GBRP,
  248. AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
  249. };
  250. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  251. if (!fmts_list)
  252. return AVERROR(ENOMEM);
  253. return ff_set_common_formats(ctx, fmts_list);
  254. }
  255. static int config_input(AVFilterLink *inlink)
  256. {
  257. AVFilterContext *ctx = inlink->dst;
  258. PP7Context *pp7 = ctx->priv;
  259. const int h = FFALIGN(inlink->h + 16, 16);
  260. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  261. pp7->hsub = desc->log2_chroma_w;
  262. pp7->vsub = desc->log2_chroma_h;
  263. pp7->temp_stride = FFALIGN(inlink->w + 16, 16);
  264. pp7->src = av_malloc_array(pp7->temp_stride, (h + 8) * sizeof(uint8_t));
  265. if (!pp7->src)
  266. return AVERROR(ENOMEM);
  267. init_thres2(pp7);
  268. switch (pp7->mode) {
  269. case 0: pp7->requantize = hardthresh_c; break;
  270. case 1: pp7->requantize = softthresh_c; break;
  271. default:
  272. case 2: pp7->requantize = mediumthresh_c; break;
  273. }
  274. pp7->dctB = dctB_c;
  275. if (ARCH_X86)
  276. ff_pp7_init_x86(pp7);
  277. return 0;
  278. }
  279. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  280. {
  281. AVFilterContext *ctx = inlink->dst;
  282. PP7Context *pp7 = ctx->priv;
  283. AVFilterLink *outlink = ctx->outputs[0];
  284. AVFrame *out = in;
  285. int qp_stride = 0;
  286. int8_t *qp_table = NULL;
  287. if (!pp7->qp) {
  288. int ret = ff_qp_table_extract(in, &qp_table, &qp_stride, NULL, &pp7->qscale_type);
  289. if (ret < 0) {
  290. av_frame_free(&in);
  291. return ret;
  292. }
  293. }
  294. if (!ctx->is_disabled) {
  295. const int cw = AV_CEIL_RSHIFT(inlink->w, pp7->hsub);
  296. const int ch = AV_CEIL_RSHIFT(inlink->h, pp7->vsub);
  297. /* get a new frame if in-place is not possible or if the dimensions
  298. * are not multiple of 8 */
  299. if (!av_frame_is_writable(in) || (inlink->w & 7) || (inlink->h & 7)) {
  300. const int aligned_w = FFALIGN(inlink->w, 8);
  301. const int aligned_h = FFALIGN(inlink->h, 8);
  302. out = ff_get_video_buffer(outlink, aligned_w, aligned_h);
  303. if (!out) {
  304. av_frame_free(&in);
  305. av_freep(&qp_table);
  306. return AVERROR(ENOMEM);
  307. }
  308. av_frame_copy_props(out, in);
  309. out->width = in->width;
  310. out->height = in->height;
  311. }
  312. if (qp_table || pp7->qp) {
  313. filter(pp7, out->data[0], in->data[0], out->linesize[0], in->linesize[0],
  314. inlink->w, inlink->h, qp_table, qp_stride, 1);
  315. filter(pp7, out->data[1], in->data[1], out->linesize[1], in->linesize[1],
  316. cw, ch, qp_table, qp_stride, 0);
  317. filter(pp7, out->data[2], in->data[2], out->linesize[2], in->linesize[2],
  318. cw, ch, qp_table, qp_stride, 0);
  319. emms_c();
  320. }
  321. }
  322. if (in != out) {
  323. if (in->data[3])
  324. av_image_copy_plane(out->data[3], out->linesize[3],
  325. in ->data[3], in ->linesize[3],
  326. inlink->w, inlink->h);
  327. av_frame_free(&in);
  328. }
  329. av_freep(&qp_table);
  330. return ff_filter_frame(outlink, out);
  331. }
  332. static av_cold void uninit(AVFilterContext *ctx)
  333. {
  334. PP7Context *pp7 = ctx->priv;
  335. av_freep(&pp7->src);
  336. }
  337. static const AVFilterPad pp7_inputs[] = {
  338. {
  339. .name = "default",
  340. .type = AVMEDIA_TYPE_VIDEO,
  341. .config_props = config_input,
  342. .filter_frame = filter_frame,
  343. },
  344. { NULL }
  345. };
  346. static const AVFilterPad pp7_outputs[] = {
  347. {
  348. .name = "default",
  349. .type = AVMEDIA_TYPE_VIDEO,
  350. },
  351. { NULL }
  352. };
  353. AVFilter ff_vf_pp7 = {
  354. .name = "pp7",
  355. .description = NULL_IF_CONFIG_SMALL("Apply Postprocessing 7 filter."),
  356. .priv_size = sizeof(PP7Context),
  357. .uninit = uninit,
  358. .query_formats = query_formats,
  359. .inputs = pp7_inputs,
  360. .outputs = pp7_outputs,
  361. .priv_class = &pp7_class,
  362. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  363. };