History

avfilter/vf_v360: add facebook's format

Eugene Lyapustin committed 7 years ago

Commit 20a12441 parent 95780f42 files · +165 −0Browse files

Showing the changes to libavfilter (1 of 2 files). Show all

Unified · Split

libavfilter/vf_v360.c+162−0
@@ -48,6 +48,7 @@ enum Projections { · 47 unmodified lines
48 EQUIANGULAR,48 EQUIANGULAR,
49 FLAT,49 FLAT,
50 DUAL_FISHEYE,50 DUAL_FISHEYE,
51 FACEBOOK,
51 NB_PROJECTIONS,52 NB_PROJECTIONS,
52};53};
53 54
@@ -137,12 +138,14 @@ static const AVOption v360_options[] = { · 83 unmodified lines
137 { "c6x1", "cubemap6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "in" },138 { "c6x1", "cubemap6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "in" },
138 { "eac", "equi-angular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "in" },139 { "eac", "equi-angular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "in" },
139 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "in" },140 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "in" },
141 { "fb", "facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=FACEBOOK}, 0, 0, FLAGS, "in" },
140 { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },142 { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
141 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },143 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
142 { "c3x2", "cubemap3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },144 { "c3x2", "cubemap3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },
143 { "c6x1", "cubemap6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },145 { "c6x1", "cubemap6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },
144 { "eac", "equi-angular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },146 { "eac", "equi-angular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },
145 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },147 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
148 { "fb", "facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=FACEBOOK}, 0, 0, FLAGS, "out" },
146 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },149 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
147 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },150 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
148 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },151 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
@@ -1646,6 +1649,153 @@ static void xyz_to_dfisheye(const V360Context *s, · 1497 unmodified lines
1646 }1649 }
1647}1650}
1648 1651
1652/**
1653 * Calculate 3D coordinates on sphere for corresponding frame position in facebook's format.
1654 *
1655 * @param s filter context
1656 * @param i horizontal position on frame [0, height)
1657 * @param j vertical position on frame [0, width)
1658 * @param width frame width
1659 * @param height frame height
1660 * @param vec coordinates on sphere
1661 */
1662static void fb_to_xyz(const V360Context *s,
1663 int i, int j, int width, int height,
1664 float *vec)
1665{
1666 const float scale = 0.99f;
1667 float l_x, l_y, l_z;
1668
1669 if (i < 4 * width / 5) {
1670 const float theta_range = M_PI / 4.f;
1671
1672 const int ew = 4 * width / 5;
1673 const int eh = height;
1674
1675 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
1676 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
1677
1678 const float sin_phi = sinf(phi);
1679 const float cos_phi = cosf(phi);
1680 const float sin_theta = sinf(theta);
1681 const float cos_theta = cosf(theta);
1682
1683 l_x = cos_theta * sin_phi;
1684 l_y = -sin_theta;
1685 l_z = -cos_theta * cos_phi;
1686 } else {
1687 const int ew = width / 5;
1688 const int eh = height / 2;
1689
1690 float uf, vf;
1691 float norm;
1692
1693 if (j < eh) { // UP
1694 uf = 2.f * (i - 4 * ew) / ew - 1.f;
1695 vf = 2.f * (j ) / eh - 1.f;
1696
1697 uf /= scale;
1698 vf /= scale;
1699
1700 l_x = uf;
1701 l_y = 1.f;
1702 l_z = -vf;
1703 } else { // DOWN
1704 uf = 2.f * (i - 4 * ew) / ew - 1.f;
1705 vf = 2.f * (j - eh) / eh - 1.f;
1706
1707 uf /= scale;
1708 vf /= scale;
1709
1710 l_x = uf;
1711 l_y = -1.f;
1712 l_z = vf;
1713 }
1714
1715 norm = sqrtf(l_x * l_x + l_y * l_y + l_z * l_z);
1716
1717 l_x /= norm;
1718 l_y /= norm;
1719 l_z /= norm;
1720 }
1721
1722 vec[0] = l_x;
1723 vec[1] = l_y;
1724 vec[2] = l_z;
1725}
1726
1727/**
1728 * Calculate frame position in facebook's format for corresponding 3D coordinates on sphere.
1729 *
1730 * @param s filter context
1731 * @param vec coordinates on sphere
1732 * @param width frame width
1733 * @param height frame height
1734 * @param us horizontal coordinates for interpolation window
1735 * @param vs vertical coordinates for interpolation window
1736 * @param du horizontal relative coordinate
1737 * @param dv vertical relative coordinate
1738 */
1739static void xyz_to_fb(const V360Context *s,
1740 const float *vec, int width, int height,
1741 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1742{
1743 const float scale = 0.99f;
1744
1745 const float phi = atan2f(vec[0], -vec[2]);
1746 const float theta = asinf(-vec[1]);
1747 const float theta_range = M_PI / 4.f;
1748
1749 int ew, eh;
1750 int u_shift, v_shift;
1751 float uf, vf;
1752 int ui, vi;
1753 int i, j;
1754
1755 if (theta > -theta_range && theta < theta_range) {
1756 ew = 4 * width / 5;
1757 eh = height;
1758
1759 u_shift = 0;
1760 v_shift = 0;
1761
1762 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
1763 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
1764 } else {
1765 ew = width / 5;
1766 eh = height / 2;
1767
1768 u_shift = 4 * ew;
1769
1770 if (theta < 0.f) { // UP
1771 uf = vec[0] / vec[1];
1772 vf = -vec[2] / vec[1];
1773 v_shift = 0;
1774 } else { // DOWN
1775 uf = -vec[0] / vec[1];
1776 vf = -vec[2] / vec[1];
1777 v_shift = eh;
1778 }
1779
1780 uf = 0.5f * ew * (uf * scale + 1.f);
1781 vf = 0.5f * eh * (vf * scale + 1.f);
1782 }
1783
1784 ui = floorf(uf);
1785 vi = floorf(vf);
1786
1787 *du = uf - ui;
1788 *dv = vf - vi;
1789
1790 for (i = -1; i < 3; i++) {
1791 for (j = -1; j < 3; j++) {
1792 us[i + 1][j + 1] = u_shift + av_clip(ui + j, 0, ew - 1);
1793 vs[i + 1][j + 1] = v_shift + av_clip(vi + i, 0, eh - 1);
1794 }
1795 }
1796
1797}
1798
1649/**1799/**
1650 * Calculate rotation matrix for yaw/pitch/roll angles.1800 * Calculate rotation matrix for yaw/pitch/roll angles.
1651 */1801 */
@@ -1789,6 +1939,12 @@ static int config_output(AVFilterLink *outlink) · 137 unmodified lines
1789 wf = inlink->w;1939 wf = inlink->w;
1790 hf = inlink->h;1940 hf = inlink->h;
1791 break;1941 break;
1942 case FACEBOOK:
1943 in_transform = xyz_to_fb;
1944 err = 0;
1945 wf = inlink->w / 5.f * 4.f;
1946 hf = inlink->h;
1947 break;
1792 default:1948 default:
1793 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");1949 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
1794 return AVERROR_BUG;1950 return AVERROR_BUG;
@@ -1832,6 +1988,12 @@ static int config_output(AVFilterLink *outlink) · 37 unmodified lines
1832 case DUAL_FISHEYE:1988 case DUAL_FISHEYE:
1833 av_log(ctx, AV_LOG_ERROR, "Dual fisheye format is not accepted as output.\n");1989 av_log(ctx, AV_LOG_ERROR, "Dual fisheye format is not accepted as output.\n");
1834 return AVERROR(EINVAL);1990 return AVERROR(EINVAL);
1991 case FACEBOOK:
1992 out_transform = fb_to_xyz;
1993 err = 0;
1994 w = roundf(wf / 4.f * 5.f);
1995 h = roundf(hf);
1996 break;
1835 default:1997 default:
1836 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");1998 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
1837 return AVERROR_BUG;1999 return AVERROR_BUG;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t