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
14KB

  1. /*
  2. * Copyright (c) 2018 Ronald S. Bultje <rsbultje gmail com>
  3. * Copyright (c) 2018 James Almer <jamrial gmail com>
  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. #include <dav1d/dav1d.h>
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/mastering_display_metadata.h"
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/opt.h"
  26. #include "avcodec.h"
  27. #include "decode.h"
  28. #include "internal.h"
  29. typedef struct Libdav1dContext {
  30. AVClass *class;
  31. Dav1dContext *c;
  32. AVBufferPool *pool;
  33. int pool_size;
  34. Dav1dData data;
  35. int tile_threads;
  36. int frame_threads;
  37. int apply_grain;
  38. int operating_point;
  39. int all_layers;
  40. } Libdav1dContext;
  41. static const enum AVPixelFormat pix_fmt[][3] = {
  42. [DAV1D_PIXEL_LAYOUT_I400] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12 },
  43. [DAV1D_PIXEL_LAYOUT_I420] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12 },
  44. [DAV1D_PIXEL_LAYOUT_I422] = { AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12 },
  45. [DAV1D_PIXEL_LAYOUT_I444] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12 },
  46. };
  47. static const enum AVPixelFormat pix_fmt_rgb[3] = {
  48. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
  49. };
  50. static void libdav1d_log_callback(void *opaque, const char *fmt, va_list vl)
  51. {
  52. AVCodecContext *c = opaque;
  53. av_vlog(c, AV_LOG_ERROR, fmt, vl);
  54. }
  55. static int libdav1d_picture_allocator(Dav1dPicture *p, void *cookie)
  56. {
  57. Libdav1dContext *dav1d = cookie;
  58. enum AVPixelFormat format = pix_fmt[p->p.layout][p->seq_hdr->hbd];
  59. int ret, linesize[4], h = FFALIGN(p->p.h, 128);
  60. uint8_t *aligned_ptr, *data[4];
  61. AVBufferRef *buf;
  62. ret = av_image_fill_arrays(data, linesize, NULL, format, FFALIGN(p->p.w, 128),
  63. h, DAV1D_PICTURE_ALIGNMENT);
  64. if (ret < 0)
  65. return ret;
  66. if (ret != dav1d->pool_size) {
  67. av_buffer_pool_uninit(&dav1d->pool);
  68. // Use twice the amount of required padding bytes for aligned_ptr below.
  69. dav1d->pool = av_buffer_pool_init(ret + DAV1D_PICTURE_ALIGNMENT * 2, NULL);
  70. if (!dav1d->pool) {
  71. dav1d->pool_size = 0;
  72. return AVERROR(ENOMEM);
  73. }
  74. dav1d->pool_size = ret;
  75. }
  76. buf = av_buffer_pool_get(dav1d->pool);
  77. if (!buf)
  78. return AVERROR(ENOMEM);
  79. // libdav1d requires DAV1D_PICTURE_ALIGNMENT aligned buffers, which av_malloc()
  80. // doesn't guarantee for example when AVX is disabled at configure time.
  81. // Use the extra DAV1D_PICTURE_ALIGNMENT padding bytes in the buffer to align it
  82. // if required.
  83. aligned_ptr = (uint8_t *)FFALIGN((uintptr_t)buf->data, DAV1D_PICTURE_ALIGNMENT);
  84. ret = av_image_fill_pointers(data, format, h, aligned_ptr, linesize);
  85. if (ret < 0) {
  86. av_buffer_unref(&buf);
  87. return ret;
  88. }
  89. p->data[0] = data[0];
  90. p->data[1] = data[1];
  91. p->data[2] = data[2];
  92. p->stride[0] = linesize[0];
  93. p->stride[1] = linesize[1];
  94. p->allocator_data = buf;
  95. return 0;
  96. }
  97. static void libdav1d_picture_release(Dav1dPicture *p, void *cookie)
  98. {
  99. AVBufferRef *buf = p->allocator_data;
  100. av_buffer_unref(&buf);
  101. }
  102. static av_cold int libdav1d_init(AVCodecContext *c)
  103. {
  104. Libdav1dContext *dav1d = c->priv_data;
  105. Dav1dSettings s;
  106. int threads = (c->thread_count ? c->thread_count : av_cpu_count()) * 3 / 2;
  107. int res;
  108. av_log(c, AV_LOG_INFO, "libdav1d %s\n", dav1d_version());
  109. dav1d_default_settings(&s);
  110. s.logger.cookie = c;
  111. s.logger.callback = libdav1d_log_callback;
  112. s.allocator.cookie = dav1d;
  113. s.allocator.alloc_picture_callback = libdav1d_picture_allocator;
  114. s.allocator.release_picture_callback = libdav1d_picture_release;
  115. s.frame_size_limit = c->max_pixels;
  116. if (dav1d->apply_grain >= 0)
  117. s.apply_grain = dav1d->apply_grain;
  118. s.all_layers = dav1d->all_layers;
  119. if (dav1d->operating_point >= 0)
  120. s.operating_point = dav1d->operating_point;
  121. s.n_tile_threads = dav1d->tile_threads
  122. ? dav1d->tile_threads
  123. : FFMIN(floor(sqrt(threads)), DAV1D_MAX_TILE_THREADS);
  124. s.n_frame_threads = dav1d->frame_threads
  125. ? dav1d->frame_threads
  126. : FFMIN(ceil(threads / s.n_tile_threads), DAV1D_MAX_FRAME_THREADS);
  127. av_log(c, AV_LOG_DEBUG, "Using %d frame threads, %d tile threads\n",
  128. s.n_frame_threads, s.n_tile_threads);
  129. res = dav1d_open(&dav1d->c, &s);
  130. if (res < 0)
  131. return AVERROR(ENOMEM);
  132. return 0;
  133. }
  134. static void libdav1d_flush(AVCodecContext *c)
  135. {
  136. Libdav1dContext *dav1d = c->priv_data;
  137. dav1d_data_unref(&dav1d->data);
  138. dav1d_flush(dav1d->c);
  139. }
  140. static void libdav1d_data_free(const uint8_t *data, void *opaque) {
  141. AVBufferRef *buf = opaque;
  142. av_buffer_unref(&buf);
  143. }
  144. static void libdav1d_user_data_free(const uint8_t *data, void *opaque) {
  145. av_assert0(data == opaque);
  146. av_free(opaque);
  147. }
  148. static int libdav1d_receive_frame(AVCodecContext *c, AVFrame *frame)
  149. {
  150. Libdav1dContext *dav1d = c->priv_data;
  151. Dav1dData *data = &dav1d->data;
  152. Dav1dPicture pic = { 0 }, *p = &pic;
  153. int res;
  154. if (!data->sz) {
  155. AVPacket pkt = { 0 };
  156. res = ff_decode_get_packet(c, &pkt);
  157. if (res < 0 && res != AVERROR_EOF)
  158. return res;
  159. if (pkt.size) {
  160. res = dav1d_data_wrap(data, pkt.data, pkt.size, libdav1d_data_free, pkt.buf);
  161. if (res < 0) {
  162. av_packet_unref(&pkt);
  163. return res;
  164. }
  165. data->m.timestamp = pkt.pts;
  166. data->m.offset = pkt.pos;
  167. data->m.duration = pkt.duration;
  168. pkt.buf = NULL;
  169. av_packet_unref(&pkt);
  170. if (c->reordered_opaque != AV_NOPTS_VALUE) {
  171. uint8_t *reordered_opaque = av_malloc(sizeof(c->reordered_opaque));
  172. if (!reordered_opaque) {
  173. dav1d_data_unref(data);
  174. return AVERROR(ENOMEM);
  175. }
  176. memcpy(reordered_opaque, &c->reordered_opaque, sizeof(c->reordered_opaque));
  177. res = dav1d_data_wrap_user_data(data, reordered_opaque,
  178. libdav1d_user_data_free, reordered_opaque);
  179. if (res < 0) {
  180. av_free(reordered_opaque);
  181. dav1d_data_unref(data);
  182. return res;
  183. }
  184. }
  185. }
  186. }
  187. res = dav1d_send_data(dav1d->c, data);
  188. if (res < 0) {
  189. if (res == AVERROR(EINVAL))
  190. res = AVERROR_INVALIDDATA;
  191. if (res != AVERROR(EAGAIN))
  192. return res;
  193. }
  194. res = dav1d_get_picture(dav1d->c, p);
  195. if (res < 0) {
  196. if (res == AVERROR(EINVAL))
  197. res = AVERROR_INVALIDDATA;
  198. else if (res == AVERROR(EAGAIN) && c->internal->draining)
  199. res = AVERROR_EOF;
  200. return res;
  201. }
  202. av_assert0(p->data[0] && p->allocator_data);
  203. // This requires the custom allocator above
  204. frame->buf[0] = av_buffer_ref(p->allocator_data);
  205. if (!frame->buf[0]) {
  206. dav1d_picture_unref(p);
  207. return AVERROR(ENOMEM);
  208. }
  209. frame->data[0] = p->data[0];
  210. frame->data[1] = p->data[1];
  211. frame->data[2] = p->data[2];
  212. frame->linesize[0] = p->stride[0];
  213. frame->linesize[1] = p->stride[1];
  214. frame->linesize[2] = p->stride[1];
  215. c->profile = p->seq_hdr->profile;
  216. c->level = ((p->seq_hdr->operating_points[0].major_level - 2) << 2)
  217. | p->seq_hdr->operating_points[0].minor_level;
  218. frame->width = p->p.w;
  219. frame->height = p->p.h;
  220. if (c->width != p->p.w || c->height != p->p.h) {
  221. res = ff_set_dimensions(c, p->p.w, p->p.h);
  222. if (res < 0)
  223. goto fail;
  224. }
  225. switch (p->seq_hdr->chr) {
  226. case DAV1D_CHR_VERTICAL:
  227. frame->chroma_location = c->chroma_sample_location = AVCHROMA_LOC_LEFT;
  228. break;
  229. case DAV1D_CHR_COLOCATED:
  230. frame->chroma_location = c->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
  231. break;
  232. }
  233. frame->colorspace = c->colorspace = (enum AVColorSpace) p->seq_hdr->mtrx;
  234. frame->color_primaries = c->color_primaries = (enum AVColorPrimaries) p->seq_hdr->pri;
  235. frame->color_trc = c->color_trc = (enum AVColorTransferCharacteristic) p->seq_hdr->trc;
  236. frame->color_range = c->color_range = p->seq_hdr->color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  237. if (p->p.layout == DAV1D_PIXEL_LAYOUT_I444 &&
  238. p->seq_hdr->mtrx == DAV1D_MC_IDENTITY &&
  239. p->seq_hdr->pri == DAV1D_COLOR_PRI_BT709 &&
  240. p->seq_hdr->trc == DAV1D_TRC_SRGB)
  241. frame->format = c->pix_fmt = pix_fmt_rgb[p->seq_hdr->hbd];
  242. else
  243. frame->format = c->pix_fmt = pix_fmt[p->p.layout][p->seq_hdr->hbd];
  244. if (p->m.user_data.data)
  245. memcpy(&frame->reordered_opaque, p->m.user_data.data, sizeof(frame->reordered_opaque));
  246. else
  247. frame->reordered_opaque = AV_NOPTS_VALUE;
  248. if (p->seq_hdr->num_units_in_tick && p->seq_hdr->time_scale) {
  249. av_reduce(&c->framerate.den, &c->framerate.num,
  250. p->seq_hdr->num_units_in_tick, p->seq_hdr->time_scale, INT_MAX);
  251. if (p->seq_hdr->equal_picture_interval)
  252. c->ticks_per_frame = p->seq_hdr->num_ticks_per_picture;
  253. }
  254. // match timestamps and packet size
  255. frame->pts = frame->best_effort_timestamp = p->m.timestamp;
  256. #if FF_API_PKT_PTS
  257. FF_DISABLE_DEPRECATION_WARNINGS
  258. frame->pkt_pts = p->m.timestamp;
  259. FF_ENABLE_DEPRECATION_WARNINGS
  260. #endif
  261. frame->pkt_dts = p->m.timestamp;
  262. frame->pkt_pos = p->m.offset;
  263. frame->pkt_size = p->m.size;
  264. frame->pkt_duration = p->m.duration;
  265. frame->key_frame = p->frame_hdr->frame_type == DAV1D_FRAME_TYPE_KEY;
  266. switch (p->frame_hdr->frame_type) {
  267. case DAV1D_FRAME_TYPE_KEY:
  268. case DAV1D_FRAME_TYPE_INTRA:
  269. frame->pict_type = AV_PICTURE_TYPE_I;
  270. break;
  271. case DAV1D_FRAME_TYPE_INTER:
  272. frame->pict_type = AV_PICTURE_TYPE_P;
  273. break;
  274. case DAV1D_FRAME_TYPE_SWITCH:
  275. frame->pict_type = AV_PICTURE_TYPE_SP;
  276. break;
  277. default:
  278. res = AVERROR_INVALIDDATA;
  279. goto fail;
  280. }
  281. if (p->mastering_display) {
  282. AVMasteringDisplayMetadata *mastering = av_mastering_display_metadata_create_side_data(frame);
  283. if (!mastering) {
  284. res = AVERROR(ENOMEM);
  285. goto fail;
  286. }
  287. for (int i = 0; i < 3; i++) {
  288. mastering->display_primaries[i][0] = av_make_q(p->mastering_display->primaries[i][0], 1 << 16);
  289. mastering->display_primaries[i][1] = av_make_q(p->mastering_display->primaries[i][1], 1 << 16);
  290. }
  291. mastering->white_point[0] = av_make_q(p->mastering_display->white_point[0], 1 << 16);
  292. mastering->white_point[1] = av_make_q(p->mastering_display->white_point[1], 1 << 16);
  293. mastering->max_luminance = av_make_q(p->mastering_display->max_luminance, 1 << 8);
  294. mastering->min_luminance = av_make_q(p->mastering_display->min_luminance, 1 << 14);
  295. mastering->has_primaries = 1;
  296. mastering->has_luminance = 1;
  297. }
  298. if (p->content_light) {
  299. AVContentLightMetadata *light = av_content_light_metadata_create_side_data(frame);
  300. if (!light) {
  301. res = AVERROR(ENOMEM);
  302. goto fail;
  303. }
  304. light->MaxCLL = p->content_light->max_content_light_level;
  305. light->MaxFALL = p->content_light->max_frame_average_light_level;
  306. }
  307. res = 0;
  308. fail:
  309. dav1d_picture_unref(p);
  310. if (res < 0)
  311. av_frame_unref(frame);
  312. return res;
  313. }
  314. static av_cold int libdav1d_close(AVCodecContext *c)
  315. {
  316. Libdav1dContext *dav1d = c->priv_data;
  317. av_buffer_pool_uninit(&dav1d->pool);
  318. dav1d_data_unref(&dav1d->data);
  319. dav1d_close(&dav1d->c);
  320. return 0;
  321. }
  322. #define OFFSET(x) offsetof(Libdav1dContext, x)
  323. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  324. static const AVOption libdav1d_options[] = {
  325. { "tilethreads", "Tile threads", OFFSET(tile_threads), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, DAV1D_MAX_TILE_THREADS, VD },
  326. { "framethreads", "Frame threads", OFFSET(frame_threads), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, DAV1D_MAX_FRAME_THREADS, VD },
  327. { "filmgrain", "Apply Film Grain", OFFSET(apply_grain), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VD },
  328. { "oppoint", "Select an operating point of the scalable bitstream", OFFSET(operating_point), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 31, VD },
  329. { "alllayers", "Output all spatial layers", OFFSET(all_layers), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
  330. { NULL }
  331. };
  332. static const AVClass libdav1d_class = {
  333. .class_name = "libdav1d decoder",
  334. .item_name = av_default_item_name,
  335. .option = libdav1d_options,
  336. .version = LIBAVUTIL_VERSION_INT,
  337. };
  338. AVCodec ff_libdav1d_decoder = {
  339. .name = "libdav1d",
  340. .long_name = NULL_IF_CONFIG_SMALL("dav1d AV1 decoder by VideoLAN"),
  341. .type = AVMEDIA_TYPE_VIDEO,
  342. .id = AV_CODEC_ID_AV1,
  343. .priv_data_size = sizeof(Libdav1dContext),
  344. .init = libdav1d_init,
  345. .close = libdav1d_close,
  346. .flush = libdav1d_flush,
  347. .receive_frame = libdav1d_receive_frame,
  348. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
  349. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_SETS_PKT_DTS,
  350. .priv_class = &libdav1d_class,
  351. .wrapper_name = "libdav1d",
  352. };