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.

364 lines
12KB

  1. /*
  2. * Copyright (c) 2013 Vittorio Giovara
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Generate a frame packed video, by combining two views in a single surface.
  23. */
  24. #include <string.h>
  25. #include "libavutil/imgutils.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "libavutil/rational.h"
  29. #include "libavutil/stereo3d.h"
  30. #include "avfilter.h"
  31. #include "formats.h"
  32. #include "internal.h"
  33. #include "video.h"
  34. #define LEFT 0
  35. #define RIGHT 1
  36. typedef struct FramepackContext {
  37. const AVClass *class;
  38. const AVPixFmtDescriptor *pix_desc; ///< agreed pixel format
  39. enum AVStereo3DType format; ///< frame pack type output
  40. AVFrame *input_views[2]; ///< input frames
  41. int64_t double_pts; ///< new pts for frameseq mode
  42. } FramepackContext;
  43. static const enum AVPixelFormat formats_supported[] = {
  44. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
  45. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVJ420P,
  46. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  47. AV_PIX_FMT_NONE
  48. };
  49. static int query_formats(AVFilterContext *ctx)
  50. {
  51. // this will ensure that formats are the same on all pads
  52. ff_set_common_formats(ctx, ff_make_format_list(formats_supported));
  53. return 0;
  54. }
  55. static av_cold void framepack_uninit(AVFilterContext *ctx)
  56. {
  57. FramepackContext *s = ctx->priv;
  58. // clean any leftover frame
  59. av_frame_free(&s->input_views[LEFT]);
  60. av_frame_free(&s->input_views[RIGHT]);
  61. }
  62. static int config_output(AVFilterLink *outlink)
  63. {
  64. AVFilterContext *ctx = outlink->src;
  65. FramepackContext *s = outlink->src->priv;
  66. int width = ctx->inputs[LEFT]->w;
  67. int height = ctx->inputs[LEFT]->h;
  68. AVRational time_base = ctx->inputs[LEFT]->time_base;
  69. // check size and fps match on the other input
  70. if (width != ctx->inputs[RIGHT]->w ||
  71. height != ctx->inputs[RIGHT]->h) {
  72. av_log(ctx, AV_LOG_ERROR,
  73. "Left and right sizes differ (%dx%d vs %dx%d).\n",
  74. width, height,
  75. ctx->inputs[RIGHT]->w, ctx->inputs[RIGHT]->h);
  76. return AVERROR_INVALIDDATA;
  77. } else if (av_cmp_q(time_base, ctx->inputs[RIGHT]->time_base) != 0) {
  78. av_log(ctx, AV_LOG_ERROR,
  79. "Left and right framerates differ (%d/%d vs %d/%d).\n",
  80. time_base.num, time_base.den,
  81. ctx->inputs[RIGHT]->time_base.num,
  82. ctx->inputs[RIGHT]->time_base.den);
  83. return AVERROR_INVALIDDATA;
  84. }
  85. s->pix_desc = av_pix_fmt_desc_get(outlink->format);
  86. if (!s->pix_desc)
  87. return AVERROR_BUG;
  88. // modify output properties as needed
  89. switch (s->format) {
  90. case AV_STEREO3D_FRAMESEQUENCE:
  91. time_base.den *= 2;
  92. s->double_pts = AV_NOPTS_VALUE;
  93. break;
  94. case AV_STEREO3D_COLUMNS:
  95. case AV_STEREO3D_SIDEBYSIDE:
  96. width *= 2;
  97. break;
  98. case AV_STEREO3D_LINES:
  99. case AV_STEREO3D_TOPBOTTOM:
  100. height *= 2;
  101. break;
  102. default:
  103. av_log(ctx, AV_LOG_ERROR, "Unknown packing mode.");
  104. return AVERROR_INVALIDDATA;
  105. }
  106. outlink->w = width;
  107. outlink->h = height;
  108. outlink->time_base = time_base;
  109. return 0;
  110. }
  111. static void horizontal_frame_pack(FramepackContext *s,
  112. AVFrame *dst,
  113. int interleaved)
  114. {
  115. int plane, i;
  116. int length = dst->width / 2;
  117. int lines = dst->height;
  118. for (plane = 0; plane < s->pix_desc->nb_components; plane++) {
  119. const uint8_t *leftp = s->input_views[LEFT]->data[plane];
  120. const uint8_t *rightp = s->input_views[RIGHT]->data[plane];
  121. uint8_t *dstp = dst->data[plane];
  122. if (plane == 1 || plane == 2) {
  123. length = -(-(dst->width / 2) >> s->pix_desc->log2_chroma_w);
  124. lines = -(-(dst->height) >> s->pix_desc->log2_chroma_h);
  125. }
  126. if (interleaved) {
  127. for (i = 0; i < lines; i++) {
  128. int j;
  129. int k = 0;
  130. for (j = 0; j < length; j++) {
  131. dstp[k++] = leftp[j];
  132. dstp[k++] = rightp[j];
  133. }
  134. dstp += dst->linesize[plane];
  135. leftp += s->input_views[LEFT]->linesize[plane];
  136. rightp += s->input_views[RIGHT]->linesize[plane];
  137. }
  138. } else {
  139. av_image_copy_plane(dst->data[plane], dst->linesize[plane],
  140. leftp, s->input_views[LEFT]->linesize[plane],
  141. length, lines);
  142. av_image_copy_plane(dst->data[plane] + length, dst->linesize[plane],
  143. rightp, s->input_views[RIGHT]->linesize[plane],
  144. length, lines);
  145. }
  146. }
  147. }
  148. static void vertical_frame_pack(FramepackContext *s,
  149. AVFrame *dst,
  150. int interleaved)
  151. {
  152. int plane, offset;
  153. int length = dst->width;
  154. int lines = dst->height / 2;
  155. for (plane = 0; plane < s->pix_desc->nb_components; plane++) {
  156. if (plane == 1 || plane == 2) {
  157. length = -(-(dst->width) >> s->pix_desc->log2_chroma_w);
  158. lines = -(-(dst->height / 2) >> s->pix_desc->log2_chroma_h);
  159. }
  160. offset = interleaved ? dst->linesize[plane] : dst->linesize[plane] * lines;
  161. av_image_copy_plane(dst->data[plane],
  162. dst->linesize[plane] << interleaved,
  163. s->input_views[LEFT]->data[plane],
  164. s->input_views[LEFT]->linesize[plane],
  165. length, lines);
  166. av_image_copy_plane(dst->data[plane] + offset,
  167. dst->linesize[plane] << interleaved,
  168. s->input_views[RIGHT]->data[plane],
  169. s->input_views[RIGHT]->linesize[plane],
  170. length, lines);
  171. }
  172. }
  173. static av_always_inline void spatial_frame_pack(FramepackContext *s, AVFrame *dst)
  174. {
  175. switch (s->format) {
  176. case AV_STEREO3D_SIDEBYSIDE:
  177. horizontal_frame_pack(s, dst, 0);
  178. break;
  179. case AV_STEREO3D_COLUMNS:
  180. horizontal_frame_pack(s, dst, 1);
  181. break;
  182. case AV_STEREO3D_TOPBOTTOM:
  183. vertical_frame_pack(s, dst, 0);
  184. break;
  185. case AV_STEREO3D_LINES:
  186. vertical_frame_pack(s, dst, 1);
  187. break;
  188. }
  189. }
  190. static int filter_frame_left(AVFilterLink *inlink, AVFrame *frame)
  191. {
  192. FramepackContext *s = inlink->dst->priv;
  193. s->input_views[LEFT] = frame;
  194. return 0;
  195. }
  196. static int filter_frame_right(AVFilterLink *inlink, AVFrame *frame)
  197. {
  198. FramepackContext *s = inlink->dst->priv;
  199. s->input_views[RIGHT] = frame;
  200. return 0;
  201. }
  202. static int request_frame(AVFilterLink *outlink)
  203. {
  204. AVFilterContext *ctx = outlink->src;
  205. FramepackContext *s = ctx->priv;
  206. AVStereo3D *stereo;
  207. int ret, i;
  208. /* get a frame on the either input, stop as soon as a video ends */
  209. for (i = 0; i < 2; i++) {
  210. if (!s->input_views[i]) {
  211. ret = ff_request_frame(ctx->inputs[i]);
  212. if (ret < 0)
  213. return ret;
  214. }
  215. }
  216. if (s->format == AV_STEREO3D_FRAMESEQUENCE) {
  217. if (s->double_pts == AV_NOPTS_VALUE)
  218. s->double_pts = s->input_views[LEFT]->pts;
  219. for (i = 0; i < 2; i++) {
  220. // set correct timestamps
  221. s->input_views[i]->pts = s->double_pts++;
  222. // set stereo3d side data
  223. stereo = av_stereo3d_create_side_data(s->input_views[i]);
  224. if (!stereo)
  225. return AVERROR(ENOMEM);
  226. stereo->type = s->format;
  227. // filter the frame and immediately relinquish its pointer
  228. ret = ff_filter_frame(outlink, s->input_views[i]);
  229. s->input_views[i] = NULL;
  230. if (ret < 0)
  231. return ret;
  232. }
  233. return ret;
  234. } else {
  235. AVFrame *dst = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  236. if (!dst)
  237. return AVERROR(ENOMEM);
  238. spatial_frame_pack(s, dst);
  239. // get any property from the original frame
  240. ret = av_frame_copy_props(dst, s->input_views[LEFT]);
  241. if (ret < 0) {
  242. av_frame_free(&dst);
  243. return ret;
  244. }
  245. for (i = 0; i < 2; i++)
  246. av_frame_free(&s->input_views[i]);
  247. // set stereo3d side data
  248. stereo = av_stereo3d_create_side_data(dst);
  249. if (!stereo) {
  250. av_frame_free(&dst);
  251. return AVERROR(ENOMEM);
  252. }
  253. stereo->type = s->format;
  254. return ff_filter_frame(outlink, dst);
  255. }
  256. }
  257. #define OFFSET(x) offsetof(FramepackContext, x)
  258. #define V AV_OPT_FLAG_VIDEO_PARAM
  259. static const AVOption options[] = {
  260. { "format", "Frame pack output format", OFFSET(format), AV_OPT_TYPE_INT,
  261. { .i64 = AV_STEREO3D_SIDEBYSIDE }, 0, INT_MAX, .flags = V, .unit = "format" },
  262. { "sbs", "Views are packed next to each other", 0, AV_OPT_TYPE_CONST,
  263. { .i64 = AV_STEREO3D_SIDEBYSIDE }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
  264. { "tab", "Views are packed on top of each other", 0, AV_OPT_TYPE_CONST,
  265. { .i64 = AV_STEREO3D_TOPBOTTOM }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
  266. { "frameseq", "Views are one after the other", 0, AV_OPT_TYPE_CONST,
  267. { .i64 = AV_STEREO3D_FRAMESEQUENCE }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
  268. { "lines", "Views are interleaved by lines", 0, AV_OPT_TYPE_CONST,
  269. { .i64 = AV_STEREO3D_LINES }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
  270. { "columns", "Views are interleaved by columns", 0, AV_OPT_TYPE_CONST,
  271. { .i64 = AV_STEREO3D_COLUMNS }, INT_MIN, INT_MAX, .flags = V, .unit = "format" },
  272. { NULL },
  273. };
  274. static const AVClass framepack_class = {
  275. .class_name = "framepack",
  276. .item_name = av_default_item_name,
  277. .option = options,
  278. .version = LIBAVUTIL_VERSION_INT,
  279. };
  280. static const AVFilterPad framepack_inputs[] = {
  281. {
  282. .name = "left",
  283. .type = AVMEDIA_TYPE_VIDEO,
  284. .filter_frame = filter_frame_left,
  285. .needs_fifo = 1,
  286. },
  287. {
  288. .name = "right",
  289. .type = AVMEDIA_TYPE_VIDEO,
  290. .filter_frame = filter_frame_right,
  291. .needs_fifo = 1,
  292. },
  293. { NULL }
  294. };
  295. static const AVFilterPad framepack_outputs[] = {
  296. {
  297. .name = "packed",
  298. .type = AVMEDIA_TYPE_VIDEO,
  299. .config_props = config_output,
  300. .request_frame = request_frame,
  301. },
  302. { NULL }
  303. };
  304. AVFilter ff_vf_framepack = {
  305. .name = "framepack",
  306. .description = NULL_IF_CONFIG_SMALL("Generate a frame packed stereoscopic video."),
  307. .priv_size = sizeof(FramepackContext),
  308. .priv_class = &framepack_class,
  309. .query_formats = query_formats,
  310. .inputs = framepack_inputs,
  311. .outputs = framepack_outputs,
  312. .uninit = framepack_uninit,
  313. };